Simple javascript prototype problem

I am trying to attach an onclick function to each tag.

I have

task.prototype.init=function(){ for (value in obj){ var Link=document.createElement('a'); Link.innerHTML='click'; Link.id=value; //I want to get the value Link.href='#' Link.onclick=this.changeName; document.body.appendChild(Link); } } task.prototype.changeName=function(){ //I want to get the clicked element id and I am not sure how to do it. return false; } 

Is there any way to accomplish this?

+1
javascript
source share
2 answers

I created an example in the fiddle: http://jsfiddle.net/dWPCS/2/

In the changeName event handler, the this reference refers to an element. Therefore, this.id returns the required value.

0
source share

Inside the event handler, this is the object that generated the event, so this should work:

 task.prototype.changeName=function() { alert(this.id); }; 
+1
source share

All Articles