How to change link HREFs via 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="" class="twitter-share-button" data-count="horizontal" data-via="theclocktwt">Tweet</a><script type="text/javascript" src=""></script></div>

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

Here you can see how it works:

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



This is what you want to do -

var yourElement = document.getElementById('yourHref');
 yourElement.setAttribute('href', '#url');

Reference the following page on the jquery site:

Should be able to use attr('href', 'value);


try this:

    function(e) {
        if(e.keyCode == 13) {
            $("#output").attr('href', cleanURL($("#url").val()));
    $('#twtbutton').prop('href', #url);
    $('#fbbutton').prop('href', #url);

This would change the 'href' property of the html element(s) with an id=twtbutton and id=fbbutton to the value stored in #url, where the value stored in #url is a string datatype. This answer utilizes the jQuery library.


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.