Search code examples
javascriptpushhrefsoundcloudpermalinks

Push permalink from JavaScript into an 'href' attribute


So I have a function that retrieves a user's SoundCloud permalink. I want it to so that onclick() of a seperate button it pushes this permalink into an href attribute in the HTML document. Any suggestions? (I simplified things by just putting a link to SoundCloud but in my app this link will actually change on every click of the 'Load' button). Thanks

HTML

<a href="" target="_blank"> SoundCloud </a>

<a onclick="loadPerma();">Load</a>

JavaScript (example)

var permalink = "www.soundcloud.com"

function loadPerma(){

   var object = {perma: permalink};
}

Solution

  • Not sure what is the use of var object = {perma: permalink};

    You can use setAttribute to set the href attribute on click of load

    var permalink = "www.soundcloud.com"
    function loadPerma(){
    document.getElementById('abc').setAttribute('href',permalink)
    }
    

    JSFIDDLE