{"id":679,"date":"2012-05-30T08:39:00","date_gmt":"2012-05-30T08:39:00","guid":{"rendered":"https:\/\/theextremewebdesigns.com\/blog\/?p=679"},"modified":"2012-05-30T08:39:00","modified_gmt":"2012-05-30T08:39:00","slug":"jquery-change-href-jquery-change-hyperlink","status":"publish","type":"post","link":"https:\/\/theextremewebdesigns.com\/blog\/jquery-change-href-jquery-change-hyperlink\/","title":{"rendered":"jQuery Change href (Hyperlink) Examples"},"content":{"rendered":"<p>At some point of time, you may be required to change the source of a hyperlink using jQuery. In this article, I am going to provide you with a simple 1 line solution to <em><strong>change href of a hyperlink dynamically using jQuery<\/strong><\/em>.<!--more--><\/p>\n<h2>How to Change href (Hyperlink) Value using jQuery &#8211; Examples<\/h2>\n<p>Let&#8217;s say that you have a hyperlink that points to hotmail.com. You now want to update the hyperlink to bing.com. You can do it\u00a0in the following way:<\/p>\n<pre class=\"lang:xhtml decode:true\">$(\"a[href='http:\/\/www.hotmail.com']\").attr('href', 'http:\/\/www.bing.com');<\/pre>\n<p>If you wan to <em><strong>change href of all links in a page to point to a custom link<\/strong><\/em> , use following code:<\/p>\n<pre class=\"lang:xhtml decode:true\">$(\"a\").attr(\"href\", \"http:\/\/www.bing.com\");<\/pre>\n<p>For jQuery version 1.6+, you can use the function &#8220;prop()&#8221; to make the change. So the above code can be rewritten as:<\/p>\n<pre class=\"lang:xhtml decode:true\">$(\"a\").prop(\"href\", \"http:\/\/www.bing.com\");<\/pre>\n<h2>attr vs prop: Which one to use?<\/h2>\n<p>attr is meant for html attributes as they are strictly defined.<\/p>\n<p>prop is mean to get property value.\u00a0\u00a0Such a property on that DOM object should exist in order to receive the value.<\/p>\n<p>For example, consider:<\/p>\n<pre class=\"lang:xhtml decode:true\">&lt;div custom = \"extreme\" &gt;\n\n$('div').attr('custom'): returns 'extreme'\n$('div').prop('custom'): returns undefined<\/pre>\n<p>As you can see, you can use &#8220;attr&#8221; to even get the value of a custom attribute that you may have defined on an element whereas using &#8220;prop&#8221; will require the property to actually be a standard property of that element.<\/p>\n<h5>That&#8217;s it!<\/h5>\n<p>Do you know of any other ways to to <em><strong>change href or change hyperlink using jQuery<\/strong><\/em>? Feel free to suggest by commenting below.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>At some point of time, you may be required to change the source of a hyperlink using jQuery. In this article, I am going to provide you with a simple 1 line solution to change href of a hyperlink dynamically using jQuery.<\/p>\n","protected":false},"author":1,"featured_media":395,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[20,13],"tags":[41],"class_list":["post-679","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-jquery-tips-and-tricks","category-tips-and-tricks","tag-jquery-tips"],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/paBnQX-aX","jetpack_sharing_enabled":true,"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts\/679","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/comments?post=679"}],"version-history":[{"count":0,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts\/679\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/media?parent=679"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/categories?post=679"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/tags?post=679"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}