3

I have this form:

<input type="checkbox" value="First" />First
<input type="checkbox" value="Second" />Second
<input type="checkbox" value="Third" />Third

<select name="first-drop">
    <option value="---">---</option>
    <option value="Option1">Option1</option>
    <option value="Option2">Option2</option>
</select>

​Is there a way (using JS/jQuery) I can change the value of first-drop to --- whenever the First check box is unchecked?

2
  • Those two paragraphs have two totally different meanings. Do you want to hide or change the value? Commented Mar 27, 2012 at 9:51
  • Change the value. Fixed the question, sorry about that confusing bit. Commented Mar 27, 2012 at 9:57

2 Answers 2

3
$('input[type="checkbox"][value="First"]').change(function (){
    $('select[name="first-drop"]').toggle();
});​

LIVE DEMO

Note that I fixed you markup:

<input type="checkbox" value="First" checked="checked" >First
<input type="checkbox" value="Second" />Second
<input type="checkbox" value="Third" />Third

<select name="first-drop">
    <option value="">---</option>
    <option value="Option1">Option1</option>
    <option value="Option2">Option2</option>
</select>​

Update:

What you want now is:

$('input[type="checkbox"][value="First"]').change(function() {
    if (!this.checked) {
        $('select[name="first-drop"] option:first').prop('selected', 'selected');
    }
});

​LIVE DEMO

If you will give your DOM elements ids it can be simple as that:

$('#first').change(function() {
    if (!this.checked) 
        $('#nothing').prop('selected', 'selected');
});

Updated DOM:

<input type="checkbox" value="First" id="first" >First
<input type="checkbox" value="Second">Second
<input type="checkbox" value="Third">Third

<select name="first-drop">
    <option value="" id="nothing" >---</option>
    <option value="Option1" >Option1</option>
    <option value="Option2">Option2</option>
</select>​

LIVE DEMO

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

1 Comment

Thank you for the markup fix! My problem is more of changing the selected value of the dropdown when the checkbox is unchecked, rather than making the dropdown disappear. My wording might be misleading, I'll revise it.
2

I added an ID to the first checkbox

<input id="first" type="checkbox" value="First">First</input>
<input type="checkbox" value="Second">Second</input>
<input type="checkbox" value="Third">Third</input>

<select name="first-drop">
    <option value="">---</option>
    <option value="Option1">Option1</option>
    <option value="Option2">$Option2</option>
</select>

<script type="text/javascript">
    $(function () {
        $("#first").click(function () {
            if ($(this)[0].checked == true) {
                $("[name='first-drop'] option:first").attr("selected", "selected");
            }
        });
    });​
</script>

Demo:

http://jsfiddle.net/8BtYc/2/

Edit: Fixed my code so that it doesn't toggle when you uncheck the first checkbox

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.