I have five select boxes .all have an first option "--Select--" and i dont i want to have select boxes have the same option selected ,no duplicate options .how to do this using jquery
-
The question is not clear, show the appropriate HTML code.Brock Adams– Brock Adams2011-08-14 09:29:05 +00:00Commented Aug 14, 2011 at 9:29
-
its about how to avoid duplicate values across the multiple select boxes using jqueryn92– n922011-08-14 09:34:04 +00:00Commented Aug 14, 2011 at 9:34
-
You use "text boxes" then "select boxes" VERY loosely. show some code.Brock Adams– Brock Adams2011-08-14 09:51:55 +00:00Commented Aug 14, 2011 at 9:51
Add a comment
|
1 Answer
First give all the selects the same class and different id's. Then something like this.
var selects = $('.days');
$('.days').change(function(){
var value = $(this).val();
var count = 0;
for(var i=0; i<selects.length; i++){
if($(this).attr('id') != $(selects[i]).attr('id')) {
var checkVal = $(selects[i]).val();
if(value == checkVal) {
alert("Value already selected, please select a different value");
$("#"+$(this).attr('id')+" option[value='']").attr("selected", "selected");
}
}
}
});
I've added a JSFiddle showing the solution.
3 Comments
Ali
the last line? If you mean
$("#"+$(this).attr('id')+" option[value='']").attr("selected", "selected"); Basically it takes the select element that has been changed and the option with the given value (blank in this case) is set as selected.n92
Suppose if i want to check for duplicate values across the multiple input text boxes, what is the logic
Ali
Do you mean you want to compare the value in an
input or textarea with the values in other inputs or textareas to see if the value is the same in more than one? Start a JS Fiddle with your html.