Setting href attribute at runtime

What is the best way to set the href attribute of the <a> tag at run time using jQuery?

Also, how do you get the value of the href attribute of the <a> tag using jQuery?

Answers


To get or set an attribute of an HTML element, you can use the element.attr() function in jQuery.

To get the href attribute, use the following code:

var a_href = $('selector').attr('href');

To set the href attribute, use the following code:

$('selector').attr('href','http://example.com');

In both cases, please use the appropriate selector. If you have set the class for the anchor element, use '.class-name' and if you have set the id for the anchor element, use '#element-id'.


Need Your Help

Flexslider 2, how to load with (document).ready function

jquery wordpress flexslider document-ready

I use the WooThemes Flexslider 2 plugin in my WordPress framework.