0

I have jsp page, which recive in model a set like this: ['a', 'c']. Set can be bigger.

We have some checkboxes for example:

<input name="a" type="checkbox">
<input name="b" type="checkbox">
<input name="c" type="checkbox">
...

If in the set the values correspondings to the <input> attribute name - we need put attribute "checked" to this <input>.

I have a solution using jQuery:

var arr = '${resource}'.replace('[', '').replace(']', '').replace(' ', '').split(','); // make array from string

for (var i = 0; i< arr.length; i++){
$('input[name='+arr[i]+']').prop('checked',true);
}

But (if it will be more simpler solution) how it can be done with JSTL tags?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Optio
  • 7,244
  • 2
  • 22
  • 30
  • By the way, your array declaration is overcomplicated. Just do `var arr = ${resource};`. JSP is just a HTML/JS code generator which doesn't run "in sync" with JS at all. – BalusC Jan 25 '16 at 15:32
  • In variant: var arr = ${resource}; alert(arr[0]) - I will recive "a is not defined". How can I use this arr in JS like an array? – Optio Jan 25 '16 at 16:06
  • That will happen if it's null or empty. – BalusC Jan 25 '16 at 16:12

0 Answers0