3

By clicking on the following DIV, nothing happens. Where is the error ?

<div onclick="function dummy(that) { alert(that.toString())}" class="next">></div>

Please help.

0

5 Answers 5

5

You are defining dummy but not calling it. I don't think it works that way, not in the HTML onclick property anyway.

I suggest you move dummy() into a separate code block:

<script type='text/javascript'>
function dummy(that) { alert(that.toString())}
</script>

and then:

<div onclick="dummy(this);" class="next">></div>

or attach the function programmatically like so:

document.getElementById("myDummyDIV").onclick = function(event) { ..... }
Sign up to request clarification or add additional context in comments.

Comments

2

This should do the trick:

<div onclick="dummy(this);" class="next"></div>

<script type="text/javascript">
function dummy(that) {
    alert(that.toString());
}
</script>

Comments

2

This is silly actually. The function you've declared is unusable as a function unless you intend to do some more fantastic stuff and call the click event of this link from other methods elsewhere. However, if you're hell-bent-for-leather intent on putting the function declaration in the onclick event, it can be done this way:

<div onclick="(function dummy(that) { alert(that.toString())})();" class="next">></div>

You end up putting the function in it's own block and then the () at the end tells the parser to do it.

Comments

2

This is a function declaration, not invocation.

You could do something like this:

(function dummy(that) { alert(that.toString())}) (event);

and the complete HTML would be:

<div onclick="(function dummy(that) { alert(that.toString())})(event);" class="next">></div>

Comments

1

you dont create function here you can just write the following

<div onclick="alert(that.toString())" class="next">></div>

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.