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);

    });

});
j08691
  • 204,283
  • 31
  • 260
  • 272
Kevin Py
  • 2,459
  • 2
  • 22
  • 33

2 Answers2

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>
Milind Anantwar
  • 81,290
  • 25
  • 94
  • 125
-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") ....

Community
  • 1
  • 1
Jaroslav Štreit
  • 415
  • 1
  • 5
  • 16