Home Set Hyperlink to a Variable- update variable all hyperlinks update
Reply: 1

Set Hyperlink to a Variable- update variable all hyperlinks update

Geo.Dude
1#
Geo.Dude Published in 2018-02-12 15:38:16Z

I have a website where there is the same hyperlink(URL) shown on multiple pages and different locations. How can I set the hyperlink to a variable, store that variable somewhere then when I need to change the URL all I need to do is update the variable resulting in all hyperlinks pointing to that variable updating as well. rather than manually changing the hyperlink on every location on the site?

Hope this makes sense,

please post answers using the following methods 1- HTML 2- JavaScript 3-JQuery 4- (something I forgot to add to list but is easy to implement)

zer00ne
2#
zer00ne Reply to 2018-02-12 16:24:33Z

There's the Document.links API which makes accessing any <a> or <area> with a href attribute very easy.

 nodeList = document.links

Demo

// Any URL
var url = `https://www.example.com/`;

// [Optional] Any text
var txt = `Example.com`;

// Reference all <a> in a NodeList called lnx
var lnx = document.links;

/* for each loop, add the url to href
|| and the text and the index number to the <a> contents (in
|| between the tags <a>...</a>). 
|| Also set each `<a>` style to act as a block
*/
for (let a = 0; a < lnx.length; a++) {

  lnx[a].href = url;
  
  // [Optional]
  lnx[a].textContent = txt +' '+ a;
  
  // [Optional]
  lnx[a].style.display = "block";

}
<a href='url'>url</a>
<a href='url'>url</a>
<a href='url'>url</a>
<a href='url'>url</a>
<a href='url'>url</a>
<a href='url'>url</a>
<a href='url'>url</a>
<a href='url'>url</a>
<a href='url'>url</a>
<a href='url'>url</a>
<a href='url'>url</a>
<a href='url'>url</a>
<a href='url'>url</a>
<a href='url'>url</a>
<a href='url'>url</a>
<a href='url'>url</a>
<a href='url'>url</a>
<a href='url'>url</a>
<a href='url'>url</a>
<a href='url'>url</a>
<a href='url'>url</a>
<a href='url'>url</a>
<a href='url'>url</a>
<a href='url'>url</a>
<a href='url'>url</a>
<a href='url'>url</a>
<a href='url'>url</a>
<a href='url'>url</a>
<a href='url'>url</a>
<a href='url'>url</a>
<a href='url'>url</a>
<a href='url'>url</a>
<a href='url'>url</a>
<a href='url'>url</a>
<a href='url'>url</a>
<a href='url'>url</a>
<a href='url'>url</a>
<a href='url'>url</a>
<a href='url'>url</a>
<a href='url'>url</a>
<a href='url'>url</a>
<a href='url'>url</a>
<a href='url'>url</a>

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.346082 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO