jeudi 30 novembre 2017

Set checkbox to be checked if input value =

I have an input and a checkbox. I have managed to change the value in the input on clicking the checkbox, and un clicking etc which works fine.

I'm looking to auto set the checkbox to be checked on pageload, if the input value = yes.

HTML

<input type="text" value = "yes" id ="inputId">
<input type="checkbox" id = "yourCheckboxId">

JQUERY

$('#yourCheckboxId').click(function() {
        if ($('#yourCheckboxId').is(':checked')){
             $('#inputId').val('yes');
        }    
        if (!$('#yourCheckboxId').is(':checked')){
             $('#inputId').val('no');
        }     
});

You'll notice that even though the value in the input is set to yes, on page load, the checkbox isn't checked. This is what I have so far, see jsfiddle here: http://ift.tt/2zT44wG

Thanks




Aucun commentaire:

Enregistrer un commentaire