medk medk - 3 months ago 13
jQuery Question

jQuery get data-parent value

I have some checkboxes that when one of them is checked returns a value stored in

data-parent


The HTML:

<input type="checkbox" name="" data-parent="some-parent">


The jQuery:

$(document).on('change', '.checkbox', function(event) {
alert($(this).data(parent));
});


Here this returns
[object Object]

Answer

you need quote parent:

$(document).on('change', '.checkbox', function(event) {
    alert($(this).data("parent"));
});

single quotes are also fine.

Comments