Hi all,
Simple question but my lack of JS experience is slowing me a bit. I would like to pass an element ID via url. Not sure how this is done but what I'm concerned is how to receive parameters via url.
Suppose the url I have is:
I understand that in the page receiving the variable I can read the url string by:
Through string manipulation I get the parameter I need. Here's the code:
Now I would like to change the background color of the element identified:
Unfortunately the color is not changing. Could someone explain why I'm not able to access the element?
Thank you very much!
Simple question but my lack of JS experience is slowing me a bit. I would like to pass an element ID via url. Not sure how this is done but what I'm concerned is how to receive parameters via url.
Suppose the url I have is:
I understand that in the page receiving the variable I can read the url string by:
Code:
var receivedID = window.location.href;
alert (receivedID);
Through string manipulation I get the parameter I need. Here's the code:
Code:
var URLLength= window.location.href.length;
var URLString = window.location.href;
var startingPos = URLString.lastIndexOf("id");
var idName = URLString.substring(startingPos+3,URLLength);
alert(idName);
Now I would like to change the background color of the element identified:
Code:
document.getElementById(idName).style.backgroundColor = "yellow";
Unfortunately the color is not changing. Could someone explain why I'm not able to access the element?
Thank you very much!