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.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.