I'm working on a magento project with a plugin changes the onclick
attribute on the (document).ready()
. I tried to access this attribute using jquery to update it after a user action but it returns undefined
. And I've noticed that the onclick attribute is NOT visible in the inspector but visible in the page source accessed by view page source
orCtrl + u
.
in inspector the targeted button looks like
<button type="button" id="addtocart_btn_5" title="Add to cart" class="button btn-cart btn-cart-with-qty">
<span>Add to cart<span>
</button>
in the page source view it looks like:
<button type="button" id="addtocart_btn_5" title="Add to cart" class="button btn-cart btn-cart-with-qty"
onclick="setLocation('http://mysite.local/eg_ar/checkout/cart/add/uenc/aHR0cDovL2hhaXJidXJzdGFyYWJpYS5sb2NhbC9lZ19hci8,/product/5/form_key/lqUtnptjQU7Cn7E1/qty/1/')">
<span>Add to cart</span>
</button>
this button is correctly accessed via the variable btn
and when i use console.log(btn.attr("onclick"));
it returns undefined
but with attribute like id
it returns the id correctly.
NOTE: the button is working very well. but I can't edit it.
Update: as per @Saptal question I've to show more code. in the html part I've this block
<div class="add-to-cart">
<form id="product_addtocart_form_15">
<span class="custom-counter">
<input min="1" name="qty" id="qty" maxlength="2" value="1" title="qty" class="qty qty-updating" type="number"><div class="custom-counter-nav"><div class="custom-counter-button custom-counter-up">+</div><div class="custom-counter-button custom-counter-down">-</div></div>
</span>
<button type="button" id="addtocart_btn_15" title="Add to cart" class="button btn-cart btn-cart-with-qty"><span>Add to cart</span></button>
</form>
</div>
and in js I do that
<script>
jQuery('.qty-updating').each(function(){
var qty = jQuery(this);
qty.on('change', function(e){
alert('here');
var btn = qty.parent().siblings('.btn-cart');
console.log(btn.attr("onclick"));
});
});
</script>
thanks in advance