tags:

views:

38

answers:

5

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.

+5  A: 

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) { ..... }
Pekka
+2  A: 

This should do the trick:

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

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

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>

jldupont
+1  A: 

you dont create function here you can just write the following

<div onclick="alert(that.toString())" class="next">></div>
peacmaker
+2  A: 

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.

Joel Etherton