0

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'

Vithursa Mahendrarajah
  • 1,194
  • 2
  • 15
  • 28

1 Answers1

1

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.

Adam Azad
  • 11,171
  • 5
  • 29
  • 70
  • `/` is obsolete in html5. – GottZ Apr 28 '18 at 14:55
  • @GottZ, I have to use those in my JSX files. – Adam Azad Apr 28 '18 at 14:56
  • What browser or framework disallows a space on id attributes? It works with a space in both react, jquery, and plain html. – Dave Newton Apr 28 '18 at 15:00
  • i see. well i'm a glimmer.js user so.. i go with the html5 spec – GottZ Apr 28 '18 at 15:02
  • @GottZ JSX isn’t HTML; tags must be closed. IMO that’s a better approach anyway. HTML 5 doesn’t disallow closing tags: https://www.w3.org/TR/html5/syntax.html#start-tags – Dave Newton Apr 28 '18 at 15:05
  • Re: attribute assignment spacing, https://www.w3.org/TR/html5/syntax.html#elements-attributes – Dave Newton Apr 28 '18 at 15:11
  • @AdamAzad Yes it works, Thanks. If I am retrieving inside loop, I tried like : $("#attribute_" + i ) where i is the iterator. But it returns null only. :( what could be the reason? – Vithursa Mahendrarajah Apr 28 '18 at 15:18
  • @user3686193, could you post where/when you're selecting the element? The code might be executed before the DOM is loaded. – Adam Azad Apr 28 '18 at 15:20
  • html5 says auto closing tags should not be closed. auto closing tags are tags like textarea or input wich don't have a true innerHTML property. it does not say there is nothing that should be closed. if you use webcomponents you are highly likely writing them in a self closing style in case you don't want the web component to yield a body. you are confusing auto closing tags with components here. – GottZ Apr 29 '18 at 19:43