Jump to content

[SOLVED] The easiest function ever (need help...)


mendoz

Recommended Posts

Hey Java freaks. :o

 

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!

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" />

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" />

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.