Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
35
rated 0 times [  38] [ 3]  / answers: 1 / hits: 19680  / 10 Years ago, tue, december 2, 2014, 12:00:00

I have a HTML Multi select box



<form action=form_action.php method=Post>
<select name=cars[] multiple id=select_id>
<option value=all id=option_id>All</option>
<option value=volvo>Volvo</option>
<option value=saab>Saab</option>
<option value=opel>Opel</option>
<option value=audi>Audi</option>
</select>
<input type=submit>
</form>


What I am trying to do is check if option All is selected or not in jQuery.



What I have tried is



<script>
$(document).ready(function()
{
$('#option_id')
.click(
function()
{
if ($(#select_id option[value='all']).length == 0) { //value does not exist so add
//Do something if not selected
}
else
{
//DO something if selected
}
//$(#select_id option:selected).removeAttr(selected); //Remove
//$(#select_id option).attr('selected', 'selected'); //Add
}
);
});
</script>


But it is not working.



Can anyone help me please?



Thanks in advance for helping.


More From » jquery

 Answers
11

Please do as below code



$('#option_id').click(function(){
if ($(#select_id option[value=all]:selected).length > 0){
alert('all is selected');
}
else{
//DO something if not selected
}
//$(#select_id option:selected).removeAttr(selected); //Remove
//$(#select_id option).attr('selected', 'selected'); //Add
});


You can check below fiddle



Multi Select


[#68628] Saturday, November 29, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
makaylahk

Total Points: 166
Total Questions: 94
Total Answers: 117

Location: Gabon
Member since Sat, Jul 25, 2020
4 Years ago
;