mendoz Posted February 28, 2007 Share Posted February 28, 2007 Hey Java freaks. This is my first ever JS function... <script type="text/javascript"> function papito(obj) { document[obj].src="images/"+obj+"_on.jpg" } function papiton(obj) { document[obj].src="images/"+obj+".jpg" } </script> When I hover over an image this function is called like this: <img name="logo2" onmouseout="papiton('logo2')" onmouseover="papito('logo2')" src="images/logo2.jpg" /> Notice that I need to set a name to the image... This has to be easier to do, right? let's see what you got! Quote Link to comment Share on other sites More sharing options...
fenway Posted February 28, 2007 Share Posted February 28, 2007 The "name" attribute doesn't make it into the document collection... only the id attribute does, and even so, that's IE-only. Best way is to use the "this" object and set its src. Quote Link to comment Share on other sites More sharing options...
mendoz Posted February 28, 2007 Author Share Posted February 28, 2007 By doing what exactly? I'm a noobbbbb Quote Link to comment Share on other sites More sharing options...
nogray Posted February 28, 2007 Share Posted February 28, 2007 you can use document.getElementById('ID_HERE') to get the object in any browser <script type="text/javascript"> function papito(obj) { document.getElementById(obj).src="images/"+obj+"_on.jpg" } function papiton(obj) { document.getElementById(obj).src="images/"+obj+".jpg" } </script> <img name="logo2" id="logo2" onmouseout="papiton('logo2')" onmouseover="papito('logo2')" src="images/logo2.jpg" /> Quote Link to comment Share on other sites More sharing options...
mendoz Posted March 1, 2007 Author Share Posted March 1, 2007 I just want to short this thing... <img name="logo2" id="logo2" onmouseout="papiton('logo2')" onmouseover="papito('logo2')" src="images/logo2.jpg" /> I think by using "this" Quote Link to comment Share on other sites More sharing options...
fenway Posted March 1, 2007 Share Posted March 1, 2007 Just use the following, it's cleaner: function swap( e, obj, name ) { if( e.type == 'mouseover' ) name += '_on'; obj.src = 'images/' + name + '.jpg'; } And then your HTML code would look like: <img onmouseout="swap(event, this, 'logo2')" onmouseover="papito(event, this, 'logo2')" src="images/logo2.jpg" /> Quote Link to comment Share on other sites More sharing options...
mendoz Posted March 1, 2007 Author Share Posted March 1, 2007 Oh yeah ! thanks! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.