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!

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.