Vithursa Mahendrarajah
Vithursa Mahendrarajah

Reputation: 1224

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'

Upvotes: 0

Views: 1325

Answers (1)

Adam Azad
Adam Azad

Reputation: 11297

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.

Upvotes: 1

Related Questions