Javascript adds ID to HTML href

I have a lifeearch script that I need to fill my html menu with information.

I have a menu that looks something like this:

<a href="?page=page&id=">Menu item</a> 

what I'm looking for is a piece of code that will do this by reference:

 <a href="?page=page&id=1">Menu item</a> 

I have Javascript that pulls the value identifier from the database table (livesearch), now I only need it to get into href on the fly.

Any suggestions?

+4
source share
2 answers
 function addPageIds(){ var links = document.getElementsByTagName("a"); for (var i = 0; i < links.length; i++){ if (/id=$/.test(links[i].href)) links[i].href += "1"; } } 
+2
source
 <a href="?page=page&id=" id="link1">Menu item</a> <script type='text/javascript'> var myidtoinsert = 5; document.getElementById("link1").href += myidtoinsert; </script> = myidtoinsert.; <a href="?page=page&id=" id="link1">Menu item</a> <script type='text/javascript'> var myidtoinsert = 5; document.getElementById("link1").href += myidtoinsert; </script> 

Give the id tag and then just use document.getElementById to get the href attribute.

+4
source

All Articles