changing HREF attribute, this code should change the HREF attribute in a Div element |
changing HREF attribute, this code should change the HREF attribute in a Div element |
Nigel needs a hand |
Apr 2 2007, 12:14 PM
Post
#1
|
Group: Members Posts: 9 Joined: 2-April 07 Member No.: 2,401 |
Hi Guys,
First post and newbie to DOM. I have the below html page and from what I understand the variable sitepoint_link has found the element 'splink' and the code below should change the href attribute. cant seem to get it working can anybody help me please? thanking you in advance Nigel CODE <html> <head> <script type="text/javascript"> var sitepoint_link = document.getElementByID('splink'); sitepoint_link.href = "http://www.google.com/"; </script> <title>New Page 1</title> </head> <body> <div id="codesection"> <p id="codepara"> </p> <ul> <li><a href="http://www.sitepoint.com/" id="splink" >SitePoint</a></li> <li><a href="http://www.yahoo.com/" id="yalink" >Yahoo!</a></li> </ul> </div> </body> </html> This post has been edited by Nigel needs a hand: Apr 2 2007, 12:22 PM |
pandy |
Apr 2 2007, 02:33 PM
Post
#2
|
🌟Computer says no🌟 Group: WDG Moderators Posts: 20,732 Joined: 9-August 06 Member No.: 6 |
Hi and welcome!
If you haven't already, turn debugging on in your browsers. The debugger should give you some hints. There are two things that stop your script from working. It's 'document.getElementById', not 'document.getElementByID'. Note the case of 'Id'. After fixing that it still won't work. As you have it the script runs as soon as the interpreter encounters it, that is before the element it should modify is created. You need to either put it after the links in the HTML or put it in a function that is run onload. |
Nigel needs a hand |
Apr 2 2007, 07:04 PM
Post
#3
|
Group: Members Posts: 9 Joined: 2-April 07 Member No.: 2,401 |
ahhhhhh yes thank you it works. I forgot that <script> can go anywhere, some reason it was stuck in my head that it can only go in the <head> part.
thanks again! |
Lo-Fi Version | Time is now: 27th April 2024 - 03:40 AM |