1

i have

<input type="checkbox" id="checkbox_1" name="artist" value="Yes">
<select id="dropdown">
<option value="All" selected="selected">- Any -</option>
<option value="0">No</option>
<option value="1">Yes</option>
</select> 

how can i select option 1 when checkbox is checked and option All when checkbox is unchecked

5 Answers 5

1
$("#checkbox_1").change(function() { 
    $("#dropdown").val(
        $(this).is(":checked") ? 
            "1" : 
            "All"
    );
});
Sign up to request clarification or add additional context in comments.

Comments

0

Working Example (jsfiddle)

$('#checkbox_1').click(function(){
    if ($(this).is(':checked')) {
        $('#dropdown').val(1);
    }
    else {
        $('#dropdown').val('All');
    }
});

Comments

0

I broke it down a bit for clarity, but this should do the trick.

$("#checkbox_1").click(function() {
    var checked = $("#checkbox_1").attr("checked");
    var selectVal;
    if (checked)
        selectVal = 1;
    else
        selectVal = "All";
    $("#dropdown").val(selectVal);
});

Comments

0

Something like this

$('#checkbox_1').change(function() {
    if($(this).is(':checked')) {
        $('#dropdown').val('1');
    } else {
        $('#dropdown').val('All');
    }
});

is(':checked') is just one of many ways to check if a checkbox is checked.

Comments

0
 if ( $('#checkbox_1').is(':checked')){
        $('#dropdown').val('1');
 }else{
     $('#dropdown').val('All');
 }

edit: this would not be a 'live' function. which other members have already provided code for

Comments

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.