Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Setting the form element values dynamically  RSS feed

 
pandu ranga
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I am trying to set the name of the text field dynamically using jsp and retrieve the same in javascript by using the getElementById function.But I am not able to get the values in the javascript.Here is the sample code which is written .Please help me in resolving the issue.

<%
for (int i=0;i< testArray.length;i++)
{
%>
<input type="text" name="pad"<%=i%> value="<%=testArray[i] %>" id="pad"<%=i%> >
<% } %>


I am setting the values in form element as in above but not able to access the same variable using getElementById function where testAtrray is some dummy array.

Regards
Pandu
 
Travis Hein
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks like you are rendering the value of i outside the name="" and id="" attribute values.

so, if you were to view source, you would see


which of course will cause all elements to have the id as "pad", and extra ignored 0,1,2,3.. within the <input> tag.

So change the line in JSP to be:
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whenever you're debugging something like this (code that writes code), it's a good idea to view the HTML source being sent to the browser. If you had done this, you probably would have spotted the 'out of place' quote marks.
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's very correct Ben. The "View Source" option *really* helps when you deal anything with UI components and JavaScripts
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!