javascript - jQuery dynamic id value returning UNDEFINED in WordPress -
i trying wordpress post id
dynamically variable in jquery
. however, returning value undefined when try debug in console
. can me on ?
here's code:
data-id attribute displaying id of current post
<?php $id = get_the_id(); ?> <button data-id="<?php echo $id; ?>" type="button" class="book btn btn-danger"> submit </button>
my jquery
jquery(document).ready(function(jquery){ jquery('.packageform').submit(packagesubmit); function packagesubmit(){ var id = jquery(this).attr('data-id'); var ceccform = jquery(this).serialize(); jquery.ajax({ type:"post", url: "wp-admin/admin-ajax.php", data: ceccform, success:function(data){ console.log(id); console.log(data); jquery(".response").html(data); jquery(".packageform")[0].reset(); jquery('body').removeclass('modal-open'); } }); return false; } });
here, using var id = jquery(this).attr('data-id');
value of attribute data-id
dynamically. it's showing message undefined value in browsing console.
any ?
here this
referring form packageform
since you're calling submit function of form, , won't have attribute you've set button.
you this
var id = jquery(this).find('button.book').data('id');
Comments
Post a Comment