Win a copy of Python Continuous Integration and Delivery this week in the Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Devaka Cooray
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Tim Holloway
  • Claude Moore
  • Stephan van Hulst
Bartenders:
  • Winston Gutkowski
  • Carey Brown
  • Frits Walraven

Populating Array values into the JavaScript function on click of a select box  RSS feed

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,

I have a Map<String,String[]> which is populated in the jsp as below:

<select id="selectTemplate" name='templateSelected' onchange="getSelectedTemplateValue(this.value)">
<c:forEach items="${templates}" var="templates">
<option value="${templates.value}">${templates.key}</option>
</c:forEach>
</select>

Now I want to get the value which is a String[] on select of the id in the above code. I am trying multiple ways to get the String[] of the selected id from the select dropdown into my javascript function of getSelectedTemplateValue and display all the elements of String[] in an alert.

Please help me in this requirement which helps me alot.
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Vijaya Parvathaneni wrote:Hello All,

I have a Map<String,String[]> which is populated in the jsp as below:



[...]



First of all, a minor quibble: I would use var="template" for the item in the forEach iteration.

If you do a "view source" on your HTML, you'll probably see that what is sent to the browser looks like



If instead you had a Map<String,List<String>> you'd probably see something more like



An <option> tag is for giving a human-friendly label and a machine-readable value; but the toString() method of an array doesn't give a value that's likely to be useful after sending to the client and returning to the server, and it gives a value that means absolutely nothing to JavaScript code.  

Depending on what you're trying to do, I suppose you could parse a toString()ed list in JavaScript.  In general, however, I'd say it's more common to use a hidden variable or AJAX to get a complex data-structure as JSON, indexed by a key of some sort.  Then your HTML source would look like, for example,



 
Whatever you say buddy! And I believe this tiny ad too:
Become a Java guru with IntelliJ IDEA
https://www.jetbrains.com/idea/
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!