0

I know there are loads of questions similar to this, but I can't find the answer I'm looking for among them.

Probably super simple: I have a very basic JS function, and it has a single argument passed to it. I just want the function to recognise that argument when used in the document.getElementById as the ID.

HTML

<div id="figure1" onmouseover="popup(figure1)"><div class="hide"> TEST </div></div>

JS Function

<script>
function popup(fig) {
    var setnow = document.getElementById( fig );
    setnow.className = "show";
}
</script>

I have tested it without using the argument, i.e.:

<script>
function popup() {
    var setnow = document.getElementById("figure1");
    setnow.className = "show";
}
</script>

... and it works as expected.

I don't use JS ever, so probably this is very simple. :-)

1
  • Why downvote? If someone asks a stupid question, that makes them novice, not worthy of a downvote...I am not a javascripter, I just needed a little help, and I did look first. Commented May 22, 2015 at 7:24

2 Answers 2

3

You need to quote the argument:

<div id="figure1" onmouseover="popup('figure1')"><div class="hide"> TEST </div></div>

Also if you want to get the same element you can use this, then you will not need to use getElementById:

<div id="figure1" onmouseover="popup(this)"><div class="hide"> TEST </div></div>
function popup(el) {
    el.className = "show";
}
Sign up to request clarification or add additional context in comments.

1 Comment

That is correct! Can't believe I didn't test that myself :-) I will mark the answer as correct in 10 mins.
1

In an eventhandler, you can use this to denote the element:

<script>
function popup() {
  this.className = "show";
}
</script>

Comments

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.