Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change onclick using Javascript?

How to change onclick using Javascript ?

https://jsfiddle.net/749rt0m7/1/

I tried to use this code, but it does not work. How can I do it?

<span  onclick="test_fn('aaa,bbb')" id="xx">edit</span>
<script>
    document.getElementById('xx').onclick = "test_fn(111,222)";
</script>
like image 387
mongkon mamiw Avatar asked Mar 16 '17 02:03

mongkon mamiw


People also ask

Can we use onclick in JavaScript?

Basic onclick syntax Note that the onclick attribute is purely JavaScript. The value it takes, which is the function you want to execute, says it all, as it is invoked right within the opening tag.


1 Answers

document.getElementById('xx').onclick = function() {
  test_fn(111, 222);
};

When setting onclick in HTML, use a string. When setting it through JS, use a function.

like image 96
Kyle Lin Avatar answered Sep 29 '22 00:09

Kyle Lin