I made a code in which I am trying to add the quantity of the product when a user types in the quantity he.she wants for that product. the problem is I am not able to select the anchor via jquery to get the href link so that I can add new value to it.
My code:
$('.mival').keyup(function(event) {
var i=$(this).parents(".cart").next('.milink').attr('href');
alert(i);
});
When I try to alert it says undefined
My Markup of html:
<div class="cart">
<div><strong>Price:</strong> <span>$20.30</span></div>
<div><strong> Qty:</strong><span>
<input type="text" class="mival" value="1" />
</span></div>
<a class="milink" href="http://example.com/vid=3">Add to Cart </a> </div>
I have not yet written the code for adding the value to the link I can do that. the problem is how to get value from this markup's a element so that I can get the href link .
You do not have any parent with class cart, using div instead can do the trick. Alternatively you can use closest('.container')
Live Demo
$('.mival').keyup(function(event) {
var i=$(this).parents("div").next('.milink').attr('href');
alert(i);
});
You also have a missing > at the end of <div class="container".
$(this).closest(".container").children('a.milink')
ex:
$('.mival').keyup(function(event) {
var i = $(this).closest(".container").children('a.milink').attr('href');
alert(i);
});
Demo: Fiddle
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With