Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to get id of dynamically created element

Status
Not open for further replies.

jaschulz

Programmer
May 20, 2005
89
FR
I create several input elements like using this function (with a different iID argument each time):

function createResponseItem(iID) {

var newInput = document.createElement('input');
newInput.setAttribute('id','response-' + iID);
newInput.setAttribute('type','radio');
newInput.setAttribute('onclick','responseClick(event)');
myDiv.appendChild(newInput);

}

and this works fine, but if I click on one of these inputs, and then, in the responseClick(event) function, I try to query the id of the input in question, like this:

function responseClick(evt) {
alert(evt.target.id);
}

What my alert shows is:

response-function toString() { [native code] }

but that's no use in identifying which of the (dynamically created) inputs I clicked on.

What am I doing wrong here?

JAS

 
[1] If you prefer setting event handler in a format similar to setting an attribute (which is not) and need passing arguments, you can do this.
>newInput.setAttribute('onclick','responseClick(event)');
[tt]newInput.onclick=function(event) {responseClick(event));[/tt]

[2] The handler in its present suggests that you're testing on moz only. If you want to test it on ie, you need a separate approach.
[tt]
function responseClick(evt) {
if (window.event) {
alert(window.event.srcElement.id);
} else {
alert(evt.target.id);
}
}
[/tt]
 
tsuji,

I see the point about setting the event handler (and about my limited responseClick() function), but changing the way newInput is created doesn't solve my problem because the alert in responseClick still produces the useless message I described in my original post. What I need is something that will get me the id of the dynamically created element in question.

Any other suggestions?

JAS
 
I think [1] does make a difference. Do you find it not?!
 
Yes, I tested it, and it produced the same message in the alert as previously.

JAS
 
Then it is your loop. I have run a quick test as well. It makes a difference. But if the newInput is not setup proper, I can't help.
 
I am using FF2, and this is what I now have:

function createResponseItem(iID,iText) {
var cDiv = document.getElementById('contentDiv');
var newInput = document.createElement('input');
var newSpan = document.createElement('span');
var newSpanText = document.createTextNode(' ' + iText);
var newBreak = document.createElement('br');
newInput.setAttribute('id','response-' + iID);
newInput.setAttribute('type','radio');
newInput.setAttribute('name','response');
newInput.setAttribute('class','response');
//newInput.setAttribute('onclick','responseClick(event)');
newInput.onclick = function(event) { responseClick(event) };
cDiv.appendChild(newInput);
cDiv.appendChild(newSpan);
newSpan.setAttribute('id','responseText-' + iID);
newSpan.appendChild(newSpanText);
cDiv.appendChild(newBreak);
}

which is called from this loop:

for (var i = 1; i < thisItem.length ; i++) {
iStr = i.toString;
createResponseItem(iStr, thisItem); }

and the mouse click function now looks like this:

function responseClick(evt) {
if (window.event) {
alert(window.event.srcElement.id);
} else {
alert(evt.target.id);
}
return true;
}

But I still get the alert message I described in my first post.

JAS
 
On the face of it, you set span's id and the input's id all to responseText-iID for some iID. It is not a good idea! Change the span id to some other closing ressemble value, such as this.
>newSpan.setAttribute('id','responseText-' + iID);
[tt]newSpan.setAttribute('id','responseText-[red]span-[/red]' + iID);[/tt]
 
Look again. The input id is set to "response-" + iID, and the span id is set to "responseText-" + iID.

Isn't that enough difference between them?

JAS



 
I cannot reproduce the problem. Your problem lies elsewhere, not shown.
 
And I hope thisItem is an array of _string_, not some element objects of alike, and it is desired to start from i=1, with i=0 safely neglected.
 
yes, thisItem is an array of string, and the 0 item has already been accessed before the loop begins.

JAS
 
I have a quick test script. You can make your own. You should then get to know where to doubt and where should not be doubted. Without further info, I have nothing to add.
 
Mea Culpa.

I have no idea why I was using iStr = i.toString()

Must have been awfully late last night.

Thanks very much,

JAS
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top