How to get ID of tag with html generated with javascript?
I have something like this:
td = '<td id = "someId"> Text <input type = "text" name = "someName" value 开发者_运维知识库= "someValue" onchange="textChange(' + "someId" + ')"></td>';
var tag = document.createElement("tr");
tag.innerHTML = td;
document.getElementById("mainTable").appendChild(tag);
But the part:
"textChange(' + "someId" + ')"
is wrong because if i want to reference like this:
document.getElementById("someId").name
This is undefined.
How should I do it?
That's because your td
'someId' don't have the property name.
It's from your input text
.
It's hard to give you actual code, since you didn't give us actual code, but you are probably looking for something like onchange="textChange(this.parentNode.getAttribute('id'))"
That being said, this is not the best way to handle DOM manipulation. Have you considered looking into a JavaScript framework such as jQuery? These frameworks make tedious tasks like this very simple. The jQuery syntax would be something like $(this).parent().attr('id')
精彩评论