Reputation: 421
I can select items in listbox using the following jquery
$('#id option[value=<?php echo $row; ?>]').attr('selected','selected');
But it is not working for optgroup
, Any idea how to do this?
Upvotes: 1
Views: 2618
Reputation: 7159
Try this,
$('#id option[value="<?php echo $row; ?>"]').attr('selected','selected');
You have to use quotes to write php strings.
This may help you.
Upvotes: 2
Reputation: 30975
<select multiple=true>
<optgroup value="coucou" >
<option value="1">1</option>
<option value="2">2</option>
</optgroup>
<optgroup value="ciao" >
<option value="3">3</option>
<option value="4">4</option>
</optgroup>
</select>
$(' optgroup[value=coucou]').children().attr('selected','selected');
Upvotes: 0
Reputation: 934
Using php to construct javascript is not a wise practice because you can't utilize browser caching and it becomes more difficult to reuse the code. Also, optgroup
is not for selecting, the purpose of the tag is to simply group options within a select
. If you're trying to detect which optgroup
contains the user selected option
, you have to use a different approach. Please provide the markup representing the whole select
and also specify what you are trying to achieve.
Upvotes: 0