Using variable as 'id' attribute in javascript

1.3k Views Asked by At

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'

1

There are 1 best solutions below

9
Adam Azad On BEST ANSWER

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.