In a JSP file I use the following code to add attributes to elements that belongs to a particular class :
$($('.dhx_combo_input').get(0)).attr('style','width: 100%;<%=(highlight.get(0))?"background-color: #FF0000;":""%>');
$($('.dhx_combo_input').get(1)).attr('style','width: 100%;<%=(highlight.get(1))?"background-color: #FF0000;":""%>');
$($('.dhx_combo_input').get(2)).attr('style','width: 100%;<%=(highlight.get(2))?"background-color: #FF0000;":""%>');
highlight is an ArrayList<Boolean>.
I got many of those lines and only the index differ. It’s not really pretty nor concise so
I tried to replace it with a for loop but I need the index to be used in JS and Java. I’m not sure that it’s possible. Will I stay stuck with this syntax or is there a better solution?
Edit :
The loop has to be limited by the number of elements that belong to class dhx_combo_input.
Unless I’m misunderstanding your question, you can do this:
Note, I modified your jquery slightly to use
eqinstead ofgetso you don’t have to calljquerytwice.