0

I'am trying to select the empty (first) value of a dropdown select option if it does not contains the value from an another dropdown select list:

$('#FirstDropdown').change(function() {
    if ( $('#SecondDropdown option').filter(':contains(' + this.value  + ')') ){
        $('#SecondDropdown option').filter(':contains(' + this.value  + ')').prop('selected',true);
    }else{
        $("#SecondDropdown option[value='']").prop('selected',true);
    }
});

This code work well if #SecondDropdown option contains this.value but the else statement doesn't reset the dropdown list if not.

Any suggestion please ?

EDIT : The dropdown lists look like this :

<select id="FirstDropdown">
    <option value="" selected="selected">&nbsp;</option>
    <option value="VAL1">First Value</option>
    <option value="VAL2">Second Value</option>
    <option value="VAL3">Third Value</option>
    <option value="VAL4">Fourth Value</option>
</select>

<select id="SecondDropdown">
    <option value="-1">&nbsp;</option>
    <option value="12">VAL1 SELECT OPTION</option>
    <option value="15">VAL2 SELECT OPTION</option>
    <option value="10">VAL3 SELECT OPTION</option>
</select>

EDIT : Added a JsFiddle.

6
  • 1
    please share the markup Commented Feb 4, 2015 at 8:05
  • I don't understand. You want the HTML sources ? Commented Feb 4, 2015 at 8:07
  • yes. html for select elements Commented Feb 4, 2015 at 8:14
  • I have édited my question to add a JsFiddle. And to show why I need to evaluate the value with "containts" filter. Sorry for omission Commented Feb 4, 2015 at 8:27
  • posted the new answer below. check it Commented Feb 4, 2015 at 8:29

3 Answers 3

2

You do not have any option element having value=''. You need to use $("#SecondDropdown option[value='-1']").prop('selected',true); . you would also need to change the condition in if statement to this.value!='':

$('#FirstDropdown').change(function() {
  if (  this.value!='' ){
    $('#SecondDropdown option').filter(':contains(' + this.value  + ')').prop('selected',true);
}else{
    $("#SecondDropdown option[value='-1']").prop('selected',true);
}});

Working Demo

Sign up to request clarification or add additional context in comments.

2 Comments

@Milid Anantwar Thanks for this. It works well if I select the first option that contains blank value. I have edited my question cause in my case the first dropdown may contains values that could not be in the second dropdown. Sorry for omission
then using your old if statement should work for you
0

Try this :There is problem in your if condition as it is getting always true. You can use .length to check if option exist and select the option else select blank

$('#FirstDropdown').change(function() {
    if ($('#SecondDropdown option:contains('+this.value +')').length>0){
        $('#SecondDropdown option:contains('+this.value +')').prop('selected',true);
    }else{
        $("#SecondDropdown option[value='-1']").prop('selected',true);
    }
});

JSFiddle Demo

4 Comments

The if statement works well in my question. I have to evaluate the content with the jQuery filter contains. And this work well. But not in the else statement.
can you try $("#SecondDropdown option[value=]").prop('selected',true); in else statement. Please share your html with select options code
please have a look over edited answer (for else part)
Thanks. but this don't work. You still trying to evaluate the first condition. And this is not where is the problem.
0

This will work for you.
First, you have to check you this.value, because any string contains ''. Second, as if works fine, you just need to filter options by [value=-1]

Final JS:

$('#FirstDropdown').change(function() {
    var $second = $('#SecondDropdown option').filter(':contains(' + this.value  + ')');
    if (this.value && $second){
        $second.prop('selected',true);
    }else{
        $("#SecondDropdown option[value=-1]").prop('selected',true);
    }
});

1 Comment

As Milind Anantwar wrote, this work well when selection the "-1" value. Can you please read again my question. I have edited because I need to evalute the "Not contains" condition. Thanks for helping

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.