Soft.Rock
Soft.Rock

Reputation: 21

how to dynamically select an option in listbox using jquery and jstl in jsp page

Hi i have one listbox with some values. i want to select it's value based on a value in array list using JSTL. My code is something like that

<select id='lstType' name="lstType">
        <option value="abc" selected="yes">abc</option>
        <option value="bcd">bcd</option>
    <option value="efg">efg</option>
 </select>

and a arraylist having some object of entity class.

class entity{
   String str;
}

Now on jsp page i am fetching like this

$("#lstCategory option[value = <c:out value="${entity.str}"/>]").attr("selected", true);

Upvotes: 1

Views: 778

Answers (2)

mprabhat
mprabhat

Reputation: 20323

Instead of attr use prop

$('#lstType option[value="${entity.str}"]').prop("selected", "selected");

Edit:

Use above only if you are in jQuery 1.6 if using before version use

 $('#lstType option[value="${entity.str}"]').attr("selected", "selected");

Upvotes: 2

Eugene Retunsky
Eugene Retunsky

Reputation: 13139

Try to use this statement:

$('#lstType option[value="${entity.str}"]').attr("selected", "selected");

Upvotes: 1

Related Questions