www.webdeveloper.com
Results 1 to 3 of 3

Thread: getelementid with a variable

  1. #1
    Join Date
    Feb 2014
    Posts
    1

    getelementid with a variable

    Hello, I have an asp page that reloads each time a link is clicked which runs a new query selection. Because of the reload, I lose my current link information, and I want to apply a style to the last clicked link. So I am putting an id on the links. Then I just put the same id name in the asp variables
    of the link. Then I have it declared and retrieved in my asp code. All this works. So after that, I retrieve curr to lcurr, then to a new javascript variable called MyJSVar. (MyJSVar seems to get the correct id name into it) Then I am trying to use getelementid to find that link with the same id as the variable value and then put some style value to it. This does not work. Can anyone tell me what is going wrong. Here is the code used for these parts.
    I have tried just hardcoding my code to one of the link id's such as the "aa" one and it still does not work.
    Any ideas?

    <script type="text/javascript">

    window.onload = function() {
    var myJsVar='<%=lcurr%>';
    }
    function changeColor() {
    document.getElementById(myJsVar).style.color="red";

    }
    </script>
    link example
    <ul class="navigation">
    <li id="aa"><a href="myasppage.asp?group=cats&amp;curr=aa">cats</a></li>
    <li id="bb"><a href="myasppage.asp?group=dogs&amp;curr=bb">dogs</a></li>

  2. #2
    Join Date
    Mar 2009
    Posts
    430
    Where is the function "changeColor" called? Shouldn't you have:
    Code:
    window.onload = function() {
    var myJsVar='<%=lcurr%>';
    changeColor(); //here
    }

  3. #3
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,036
    You don't need
    Code:
    window.onload = function() {
    var myJsVar='<%=lcurr%>';
    }
    you only need
    Code:
    myJsVar='<%=lcurr%>';
    as the variable needs to be avaiable in the global sense, it also does not need to be nested in a window.onload function because the value of it is set on page render.

    IMHO I don't see why you can't achieve your goal by way of using CSS that is written in and set on page render.
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles