3

This onmouseover 'lookdown' function works but the onmouseout 'lookup' function does not:

function lookdown(harvid) { harvid.innerHTML="See details below"; }
function lookup(harvid,crop) {harvid.innerHTML=crop;}

<td id="harv1244" onmouseover="lookdown(harv1244)"
onmouseout="lookup(harv1244,apples)">dummy</td>

This onmouseout function works (although, of course, it prints 'crop' and I want to pass in a string as I am trying to do above):

function lookup(harvid) {harvid.innerHTML="crop";}

<td id="harv1244" onmouseover="lookdown(harv1244)"
onmouseout="lookup(harv1244)">dummy</td>
6
  • 1
    what is apples here ? Commented Jul 15, 2014 at 2:54
  • 1
    Avoid this, really. You're relying on the browser creating global variables for your ids, and inline events are a source of errors. Add your events in JavaScript, querying your elements from the DOM and then using addEventListener. Commented Jul 15, 2014 at 2:54
  • 1
    apples isn't a string, but a variable. Does it hold a string value? Commented Jul 15, 2014 at 3:00
  • Mritunjay, apples is the word that I am passing to the lookup() function that I want inserted using innerHTML. Commented Jul 15, 2014 at 3:19
  • elclans, I don't know what any of that means. Commented Jul 15, 2014 at 3:21

1 Answer 1

4

There are several issues with your code:

  • You are passing undeclared variables into your functions. apples and harvid are variables, not strings, and therefore undefined. You need to put those values in quotes to make them strings

  • harvid needs to either be a string or a node element. But you are not passing in either. Assuming you want it to be a string, you then need to find the DOM element using getElementById.

Here is a working solution:

Javascript:

function lookdown(harvid) { 
    document.getElementById(harvid).innerHTML="See details below"; 
}

function lookup(harvid,crop) {
    document.getElementById(harvid).innerHTML=crop;
}

HTML:

<div id="harv1244" onmouseover="lookdown('harv1244')"
onmouseout="lookup('harv1244','apples')">
    dummy
</div>

And here the associated fiddle: http://jsfiddle.net/pQM37/

EDIT:

To make this code a little cleaner, you could pass the element itself into the function, instead of the id, like this:

Javascript:

function lookdown(ele) { 
    ele.innerHTML="See details below"; 
}

function lookup(ele,crop) {
    ele.innerHTML=crop;
}

HTML:

<div id="harv1244" onmouseover="lookdown(this)"
onmouseout="lookup(this,'apples')">
    dummy
</div>

Fiddle: http://jsfiddle.net/pQM37/1/

Sign up to request clarification or add additional context in comments.

1 Comment

That's perfect, Steve. Clear, two solutions, and a working example. Could not be better. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.