1

I have a form (with Contact Form 7 on Wordpress). This form has a feature for display a <div> when a checkbox is checked (with jQuery). I wish also that when this checkbox is checked, the select > option change this value.

eg: if the checkbox is not checked, the select > option has value "NORMAL", but if checkbox is checked, the select > option has value "EXPORT".

My Form (HTML, not Contact Form 7)

<form action="" method="post" class="wpcf7-form">
    <!-- Inputs -->
    <input name="chkCheckbox" type="checkbox" id="checkbox" class="checkboxMail" />Export Demand
    <div class="checkMailOK">
            <!-- Others inputs -->
    </div>
    <select name="mail" class="wpcf7-form-control wpcf7-select mail" id="mail">
        <option value="NORMAL">NORMAL</option>
        <option value="EXPORT">EXPORT</option>
    </select>
    <!-- Others inputs -->
</form>

My JS

$(document).ready(function() {

    $(".checkboxMail").click(function() {

        $(".checkMailOK").slideToggle(400);

    });

});
2
  • Where's the element with the class checkboxMail? Commented Apr 20, 2016 at 14:11
  • My bad. checkboxMail is the checkbox input Commented Apr 20, 2016 at 14:13

2 Answers 2

2

You need to use checked state of element in change function to set the value of option and text of div accordingly:

$(".checkboxMail").change(function() {
    var setval = this.checked ? "NORMAL" : "EXPORT";
    $(".checkMailOK").text(setval);
    $("#mail").val(setval);
});

$(function(){
$(".checkboxMail").change(function() {
    var setval = this.checked ? "NORMAL" : "EXPORT";
    $(".checkMailOK").text(setval);
    $("#mail").val(setval);
 });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="" method="post" class="wpcf7-form">
    <!-- Inputs -->
    <input name="chkCheckbox" type="checkbox" id="checkbox" class="checkboxMail" />Export Demand
    <div class="checkMailOK">
            <!-- Others inputs -->
    </div>
    <select name="mail" class="wpcf7-form-control wpcf7-select mail" id="mail">
        <option value="NORMAL">NORMAL</option>
        <option value="EXPORT">EXPORT</option>
    </select>
    <!-- Others inputs -->
</form>

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

1 Comment

it's Works ! But the setval value must be reversed. Thank you !
-1
$(".checkboxMail").on("click",function() {

Set select option 'selected', by value

});

Can be on click or change, then you can add if .checkboxMail.is(":checked") ....

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.