Usama
Usama

Reputation: 73

Cannot change href value using JQuery

I cannot change anchor tag attribute value using jquery, I cannot figure out what is the problem. I'm sharing my code

HTML:

<a class="btn btn-primary" data-ajax="true" data-ajax-method="POST" data-ajax-success="onDeleteSuccess" href="/ShoppingCart/AddToCart" id="url">Add to cart</a>

Jquery:

$(function() {
    $('#qty').on("mouseout keydown mouseover", (function() {
        if ($('#product-quantity').val() <= 0) {
            $('#product-quantity').val(1);
        }
        var triming = $('#pricetxt').text().replace('$', '');
        var price = parseInt(triming);
        var qty = $('#product-quantity').val();
        var res = price * qty;
        var b = $('#product-quantity').val();
        $('#write').text(b + ' quantity' + ' X ' + price + ' price = $' + res);
        $('#url').attr("href", '/ShoppingCart/AddToCart/3019' + '?qty=' + b);
    }));
});

It is not changing href value and I need to change it on keydown and mouseover events.

Upvotes: 1

Views: 2096

Answers (1)

gaurav
gaurav

Reputation: 230

With jQuery 1.6 and above you should use

$("#url").prop("href", "http://www.jakcms.com")

The difference between prop and attr is that attr grabs the HTML attribute where as prop grabs the DOM property.

Upvotes: 3

Related Questions