How can I insert links into HTML using javascript without the limitations of document.getElementById -


First of all, tell me what I'm trying to do. I currently have 8 short websites that are similar to header images and href links.

Every time I need to update these pages, I am looking for a way to reduce the maintenance and efficiency of human error.

For example, say I have 2 links pointing to the state-specific login page.

  & lt; a href = "https://www.mypage.com/studentLogin?stateId= WA" & gt; Student Login & lt; / A & gt; & Lt; a href = "https://www.mypage.com/teacherLogin?stateId=WA" & gt; Teacher Login & lt; / A & gt;   

In the past, I am making copies of updated HTML, then searching and replacing "State ID = WA" for "State ID = MA"

I started to see if I could make the URL using Javascript and add some 2-digit State ID using some functions. In this way, I only need to update the code, then copy it and replace the two-digit state ID in one file in one place.

I have made progress using the following external javascript file

  getParam (function) {return 'WA'; } GetLink () {document.getElementById ('studentlogin'). Href = 'https://www.mypage.com/studentLogin?stateId=' ++ getParam (); } GetLink () {document.getElementById ('Teacher Login'). Href = 'https://www.mypage.com/teacherLogin?stateId=' ++ getParam ();   

and then using it in HTML

  & lt; A href = "#" onclick = "getLink ();" Id = "studentlogin" & gt; Click me & lt; / A & gt;   

This worked, as long as I do not know that I can not find more than one element in HTML with the same ID. For example, one of the pages has a link for student login in the menu, and the main content of HTML is also a link to the same place, and only one of them will do the job.

So I think I can make several functions in the external javascript file, which has its own ID, then update the HTML to call it a new ID, but I'm looking for a better way. I am here.

is decreasing maintenance because I currently have these 8 landing pages, but we can be more in the near future. Since there are only 4 separate links from these pages, so I will be fine if I store a whole variable in a variable and this variable is

  & lt; One & gt; Tags   

Thank you for your help.

You can add classes for related links, and then document.getElementsByClassName ("myclass ") Get element with

  & lt; a href = "#" class = "Myclass" & gt; test1 & lt; / A & gt; & Lt; a href = "#" class = "myclass" & gt; Test2 & lt; / A & gt;   

and in JS:

  var link = document.getElementsByClassName ("myclass")   

This can link all the links on which you can repeat to apply your modifications.

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -