dynamically added text id, getElementById

左心房为你撑大大i 提交于 2019-12-11 09:26:01

问题


I create text box dynamically and assign its ID dynamically. in javascript if I call getElementById the alert fails, just nothing happens.

<% for(int i=0; i<lines.length;i++) {
  if(lines[i].contains(" ")) { %>
    <input type=text name='key1<%=i%>' id="idkey<%=i%>" value ="<%=abc%>"/>
                          <%
  }
} %>

Javascript :

for(j=0; j<len; j++){
  var lblElement = getElementById("idkey"+j);
  alert(lblElement);
}

回答1:


you forgot the global name document for use getElementById

document.getElementById('idkey'+j)




回答2:


You're missing document before getElementById:

for(j=0; j<lines.length; j++){
    var lblElementID = document.getElementById('idkey'+j);
    console.log(lblElementID);
}



回答3:


You forget to use the document global namespace

The correct way to access the getElementById is the following

document.getElementById('idkey')



来源:https://stackoverflow.com/questions/20704718/dynamically-added-text-id-getelementbyid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!