DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   JavaScript / DHTML / AJAX (http://www.daniweb.com/forums/forum117.html)
-   -   Dynamic ID (http://www.daniweb.com/forums/thread158990.html)

brechtjah Nov 22nd, 2008 5:24 pm
Dynamic ID
 
Hi,
I'm creating the layout for a dynamic webshop, all pictures are getting dynamic IDs to have their elements changed. I'm currently having errors with the code below and have no idea why.

the argument id is not null, however
document.getElementById(id) is. I don't understand how that can happen :S

This is my javascript code
                <script type="text/javascript">
                        function change_bg(id) {
                                if(id != null && document.getElementById(id) != null) {
                                        document.getElementById(id+'bg').src='product_bg_big.png';
                                        document.getElementById(id+'fg').src='product_fg_big.png';
                                        document.getElementById(id).src='product_i_big.png';
                                }
                                else {
                                        confirm('');
                                }
                        }
                       
                        function change_bg_back(id) {
                                if(id != null && document.getElementById(id) != null) {
                                        document.getElementById(id+'bg').src='product_bg.png';
                                        document.getElementById(id+'fg').src='product_fg.png';
                                        document.getElementById(id).src='product_i.png';
                                }
                                else {
                                        confirm('');
                                }
                        }
                </script>

This is my php code:
echo "<td>
<img id='product".$numProduct."fg' src='product_fg.png' alt='product_fg'  onmouseover=javascript:change_bg('product".$numProduct."') onmouseout=javascript:change_bg_back('product".$numProduct."')>
                                        </td>";

EDIT: what's even stranger is that it all works, when I leave out the if, I just have a FireFox Addon which showed me those errors.
Any help would be very well appreciated.
Thanks

brechtjah Nov 22nd, 2008 5:48 pm
Re: Dynamic ID
 
Solved it myself


All times are GMT -4. The time now is 7:49 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC