1

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

Theo Strauss
  • 1,281
  • 3
  • 19
  • 32

3 Answers3

3

You can do this using the href JavaScript property.

Simply add to your JavaScript:

myElement.href = shortened;

See similar question for more reference.

Community
  • 1
  • 1
Ryan Den-Kaat
  • 118
  • 1
  • 5
  • does the question above yours work as well? i will mark both correct if they both do – Theo Strauss Mar 21 '17 at 23:06
  • 1
    @TheoStrauss both will work in the same manner. However the other answer is doing a String replace. The String replace method relies on the href initially staying as "http://phonefiveme.com/nocontact" otherwise it will not be able to replace the String correctly. – Ryan Den-Kaat Mar 21 '17 at 23:11
1

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);
Željko Krnjić
  • 2,356
  • 2
  • 17
  • 24
1

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

myElement.href=shortened;

to do the trick