Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change link HREFs via javascript

Tags:

javascript

I have a form where the user inputs an URL. The value resulting from that input after tweaking it a bit is the variable #url which is displayed to the user right away.

I need to make the like and tweet buttons in my site point to that URL. Making their HREFS equal to the variable #url

This are the share buttons:

<a id="twtbutton" href="http://twitter.com/share" class="twitter-share-button" data-count="horizontal" data-via="theclocktwt">Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script></div>

<fb:like id="fbbutton" send="false" layout="button_count" width="450" show_faces="false" font="" style="position: absolute; left:110px;"></fb:like>
</div>

Here you can see how it works: http://www.chusmix.com/tests/magic.html

What appears as "Improved link" is the url I want the share buttons to use.

like image 618
lisovaccaro Avatar asked Apr 26 '11 04:04

lisovaccaro


People also ask

How do I change a link in href?

Answer: Use the jQuery . attr() Method attr() method to dynamically set or change the value of href attribute of a link or anchor tag. This method can also be used to get the value of any attribute.

What is link HREF in JavaScript?

In JavaScript, you can call a function or snippet of JavaScript code through the HREF tag of a link. This can be useful because it means that the given JavaScript code is going to automatically run for someone clicking on the link. HREF refers to the “HREF” attribute within an A LINK tag (hyperlink in HTML).


1 Answers

This is what you want to do -

var yourElement = document.getElementById('yourHref');
 yourElement.setAttribute('href', '#url');
like image 77
Steve Avatar answered Oct 11 '22 16:10

Steve