10

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>
1
  • 2
    Note that your inline attribute is passing a single argument that is a string with a comma in it, but your attempted altered version is passing two arguments that are numbers. Commented Mar 16, 2017 at 2:41

4 Answers 4

27

There are a couple things going on here.

First of all test_fn() is not a function in your jsfiddle. You will need to define it in order for it to be called.

Second of all you are setting the onclick attribute to be a string not a function. You would have to set it to be a function using the code below.

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

Here is an example of a working jsfiddle.

UPDATE

It looks like I misunderstood the question. If you would actually like to change the onclick attribute, as in, change what the HTML shows, you will want to use setAttribute(). So your code would be..

document.getElementById('xx').setAttribute( "onClick", "test_fn('111,222')" );

Here is the updated jsfiddle

As a side note, like nnnnnn says in a comment of the original post, using this you are passing 1 parameter through the function which is the string 111,222 when it seems that you might want to be passing two. If you would like to pass 111 as the first parameter and 222 as the second parameter, you would call the function with either test_fn(111,222) (passing the parameters as integers) or with test_fn('111','222') (passing the parameters as strings)

Sign up to request clarification or add additional context in comments.

3 Comments

No when load page i want to change from onclick="test_fn('aaa,bbb')" to onclick="test_fn('111,222')"
Regarding the update, I'm not sure that you misunderstood the question so much as the OP didn't understand what they were asking for. Are there any benefits to changing the attribute to a new string rather than just assigning a function directly? I would have thought the "correct" result would be measured by which function actually gets called and with what parameters when the element is clicked, not by what the HTML "looks like", especially when "View Source" will still show the original source code.
I am unsure as to why the OP would want it done like that, however for some reason the answer that was provided to him multiples times did not seem to achieve what he wanted.
3
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.

4 Comments

No when load page i want to change from onclick="test_fn('aaa,bbb')" to onclick="test_fn('111,222')"
When i tested your code, when page i want to chage id="my-span" from onclick="alert('foo')" to onclick="alert('baz')" but still not work.
In my browser, the span alerts 'baz' when you click it, which is what you want, right? If it doesn't work for you, then what browser are you using? Does it support JS?
@mongkonmamiw - all of the answers are correct based on your question. Maybe try to further clarify? The function name, number of parameters and scope are all important... please describe what you're trying to do, not what you think is wrong with the code
0

You need to set onclick to a function, that runs your code test_fn('111,222'):

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

4 Comments

No when load page i want to change from onclick="test_fn('aaa,bbb')" to onclick="test_fn('111,222')"
yes, that's what this does. onclick in javascript needs to be a function
@mongkonmamiw I have a sneaking suspicion you are trying to rightclick > viewsource to see the change. Well that's not how browsers work. The viewsource only shows the initial/original content returned from the server. You can see the updated onclick attribute by looking at the live DOM (use your browser's dev console to view it)
Please note that in javascript using the .onclick will not change the attribute in the live DOM. In order to do this you will need to use the setAttribute function.
0

I don't see where you are defining the function test_fn you are hooking the function to the element onclick twice in the html and in js .. only one is needed , and you need to actually define the function

 <span id="xx">edit</span>
 <!-- or .. not both , they are doing pretty much the same thing--> 
 <script>
   document.getElementById('xx').onclick = function(){
     console.log('clicked')
   }
 </script>

now you need to define test_fn it will get called upon clicking the element

function test_fn(arg1 , arg2){
  condole.log(arg1 , arg2)
}

note that the html onclick version is passing one argument and the js script version is passing two argumens.

2 Comments

when i tested your code jsfiddle.net/j2mbngq1 id="xx" still onclick="test_fn('aaa,bbb')" not change to onclick="test_fn('111,222')"
edited and here is another version . jsbin.com/vutotar/edit?html,js,console,output

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.