[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1660
  • Last Modified:

Using getElementById with variables

I'm trying to change the font color of some text using the getElementById property while passing a variable as the element ID.  I need to use a variable for various reasons.  I continue to recieve errors using the following code  (Please note that PreviousPage is a global variable):

var PreviousPage = "";

function ResetPageColor(PreviousPage) {
    var fObjPrevious = document.getElementById(TextToChange);
    fObjPrevious.color = "white";
    fObjPrevious.size = "1"
}

<html>
.....
<font size="1" color="#FFFFFF" id="Page1" onclick="ResetPageColor(PreviousPage);">Page 2</font>
....
</html>
0
TheChos
Asked:
TheChos
  • 2
1 Solution
 
Pravin AsarCommented:
What is value of TextToChange ?

Is that a global variable..

If TextToChange

function ResetPageColor(PreviousPage) {
    var fObjPrevious = document.getElementById(TextToChange);
    if (!fObjPrevious) { alert ('Not found  object with id ' + TextToChange); }
    if (fObjPrevious) {
    fObjPrevious.style.color = "white";
    fObjPrevious.style.textSize = "1";
}
0
 
TheChosAuthor Commented:
I made a mistake when posting the question.  Below is the way that the code should be:

var PreviousPage = "";

function ResetPageColor(TextToChange) {
    var fObjPrevious = document.getElementById(TextToChange);
    fObjPrevious.color = "white";
    fObjPrevious.size = "1"
}
0
 
smaccariCommented:

function ResetPageColor(TextToChange) {
    var fObjPrevious = document.getElementById(TextToChange);
    fObjPrevious.color = "white";
    fObjPrevious.size = "1";
}

<font size="1" color="#FFFFFF" id="Page1" onclick="ResetPageColor(PreviousPage);">Page 2</font>

By the way, where do you initialize PreviousPage?
Cause in your example, its values is "".
0
 
TheChosAuthor Commented:
It worked great!  Believe it or not, I was setting the variable PreviousPage equal to the NAME property of a different object.  Your previous message made me realize what I was doing.  Thanks.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now