Search code examples
javascriptjqueryhyperlinkhref

how to replicate a href from javascript file


i have a link

<a id="cartLink" href="https://site.foxycart.com/cart?cart=checkout" >Test</a>

when you click on this link it loads up a popup dialog (it doesn't change to a new window location, etc)

i now want to generate that url on the fly so i thought i would use jquery and something like this:

 $('#cartLink').live('click', function () {
     var count = $("#abc").text();

     var url = "https://site.foxycart.com/cart?cart=view&MyCount=" + count;

     NOW SOMEHOW REPLICATE THE SAME WAY THE HREF WOULD HAVE WORKED

});

so as you can see i can't use window.open(), etc. what is the best way to replicate the same behavior as if i would have clicked on the link with the href set.


Solution

  • Since your selected has selected any element with id #cartLink, you'll be able to refer to it as $(this) from within your function.

    After the var url = ... ; write the following:

    $(this).attr('href', url);
    

    http://api.jquery.com/attr/