Search code examples
javascriptjqueryhrefgetelementbyid

Change href by finding id- javascript


I am very new to JS and a Swift developer. I have a button that has this property:

<a href="http://phonefiveme.com/nocontact" id="yui_3_17_2_1_1490136681073_127" style="cursor: url(&quot;chrome-extension://ledmjlnkdlappilhaaihfhanlpdjjalm/rockhand.png&quot;), auto;">

What I need to do is to edit that href to be a link that is stored in a variable. Here is what I have:

    var currentLocation = window.location;
    var stringURL = String(currentLocation);
    var shortened = stringURL.substring(32);
    var myElement = document.getElementById("yui_3_17_2_1_1490136681073_127");

I just need to replace "http://phonefiveme.com/nocontact" with shortened

Any help would be much appreciated! Thanks so much. Cheers, Theo


Solution

  • You can do this using the href JavaScript property.

    Simply add to your JavaScript:

    myElement.href = shortened;
    

    See similar question for more reference.