Passing a local variable inside a function

By clicking on the next div, nothing happens. Where is the mistake?

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

Please, help.

+3
source share
5 answers

You define dummy, but do not call it. I do not think this works like that, and not in the HTML property onclick.

I suggest you move the dummy element () to a separate block of code:

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

and then:

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

or programmatically use this function as follows:

document.getElementById("myDummyDIV").onclick = function(event) { ..... }
+5
source

This should do the trick:

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

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

, .

- :

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

HTML:

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

+2

This is actually stupid. The function you declared is unsuitable for use as a function if you are not going to do some more fantastic things and trigger the click event of this link from other methods elsewhere. However, if you intend to place a function declaration in the onclick event, this can be done as follows:

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

In the end, you put the function in your own block, and then () at the end tells the parser to do this.

+2
source

you are not creating a function here you can just write the following

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

Source: https://habr.com/ru/post/1729069/


All Articles