Search code examples
javascriptfor-loopjaggery-js

Using variable as 'id' attribute in javascript


I have following jaggery code for UI where I am fetching values from user via UI.

<%
for (i = 0; i < applicationAttributes.length; i++) {
%>
<div>
  <label>data:</label>
  <div>
      <input type="text" id = "attribute_<%= i%>" >
  </div>
</div>
<%
}
%>

when reading the values respective to each ids,

var data = $("#attribute_1").val();

But it is not returning the value, Can someone specify the proper method to assign 'id'


Solution

  • There should be no space between id and its value

    <input type="text" id = "attribute_<%= i%>" >
    <!- _________________^_^ -->
    

    It should be

    <input type="text" id="attribute_<%= i%>" />
    

    And it's a good practice to have / closing void elements.