user1001176
user1001176

Reputation: 1165

Get href link and add a value to it via jQuery

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 .

Upvotes: 3

Views: 2115

Answers (2)

Arun P Johny
Arun P Johny

Reputation: 388316

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

Upvotes: 2

Adil
Adil

Reputation: 148120

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);
});

Upvotes: 2

Related Questions