Theo Strauss
Theo Strauss

Reputation: 1351

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

Upvotes: 1

Views: 917

Answers (3)

TorfDrottel WMD
TorfDrottel WMD

Reputation: 26

you can access the properties of the html element and simply use

myElement.href=shortened;

to do the trick

Upvotes: 1

Ryan Den-Kaat
Ryan Den-Kaat

Reputation: 118

You can do this using the href JavaScript property.

Simply add to your JavaScript:

myElement.href = shortened;

See similar question for more reference.

Upvotes: 3

Željko Krnjić
Željko Krnjić

Reputation: 2366

Simple:

var currentLocation = window.location;
var stringURL = String(currentLocation);
var shortened = stringURL.substring(32);
var myElement = document.getElementById("yui_3_17_2_1_1490136681073_127");
myElement.href = myElement.href.replace("http://phonefiveme.com/nocontact", shortened);

Upvotes: 1

Related Questions