F. Vandroy F. Vandroy - 4 months ago 13
jQuery Question

A checkbox like my Select

I have a select element and I need to replace this by the checkbox.

Look at my code:

<select id="civilite" name="civilite" class="form-control" title="Merci de séléctionner une valeur.">
<!-- Choix déroulant -->
<option value="">-</option>
<option value="monsieur">Monsieur</option>
<option value="madame">Madame</option>
</select>


And a part of my JS:

civiliteVal = $('#civilite option:selected').val()

Answer Source

I guess you are looking for this https://jsfiddle.net/0rbr16zb/

$('input[name="civilite"]').change(function(){
	if($(this).is(':checked')){
    var value = $(this).val();
    console.log(value);
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" name="civilite" value="monsieur" /> Monsieur
<input type="checkbox" name="civilite" value="madame" /> Madame