lundi 24 avril 2017

Get if checkbox is checked or not in jquery

Following is my jQuery code by which I am trying to retrieve whether the checkbox is clicked or not. I used both is and prop but somehow its always returning false and undefined in console. Let me know what I am doing wrong here.

Also, let me know the more better way to handle the checkbox value is to target the click or change event on checkbox( currently I am listening to click event).

jQuery Code -

const $document = $(document);
$document.ready(() => {
    $("input[type='checkbox']").on('click', () => {
        console.log("Checked Value 'is' :: ", $(this).is(':checked'));
        console.log("Checked Value 'prop' :: ", $(this).prop('checked'));
    })
})

HTML CODE -

<form name="myForm" id="#myForm">
        <div>
            <p><input type="checkbox" name="accept-t-and-c" /> I accept terms and conditions</p>
        </div>
        <div>   
            <button type="submit" name="submit">Submit</button>
        </div>  
    </form>

JSFIDDLE - http://ift.tt/2p8w5HX




Aucun commentaire:

Enregistrer un commentaire