Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get href link and add a value to it via jQuery

Tags:

html

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 .

like image 691
user1001176 Avatar asked Feb 01 '26 05:02

user1001176


2 Answers

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);
});
like image 186
Adil Avatar answered Feb 03 '26 23:02

Adil


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

like image 36
Arun P Johny Avatar answered Feb 03 '26 23:02

Arun P Johny