0

I have an onclick event handler function like

onclick="add(this.id,x,y);" , embedded in HTML. The add function, after making its main job, has to overwrite the onclick event handler like:

function add(id,x,y) {
element = getElementById("id");
z = y*x; //just an example
element.onclick = 'add('+id+','+x+','+z+');';
}

So when next time the onclick is called add(id,x,z); should run, but unfortunately it's not working this way. Has anyone a solution for this problem?

1
  • 1
    You should consider passing this as a parameter instead of this.id; what happens if the element does not have an id? You can then alter add() as follows: function add(element, x, y) { z = y*x; element.onclick = 'add(this,' + x + ', ' + z + ');'; Commented Mar 10, 2011 at 11:33

1 Answer 1

2

This line:

element = getElementById("id");

should be:

element = document.getElementById(id); // without quotes!
Sign up to request clarification or add additional context in comments.

2 Comments

More specifically, it's getting the element which has the actual name "id", rather than the value of the id parameter.
I did some further testing. Any value that is assigned to element.onclick, string, integer, function, whatever, stoppes my script. So when i put an alert('asd'); after the assignment for testint, that alert() never runs :S

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.