How do I use .getElementById(). dynamicly?

Discussion in 'JavaScript' started by Imozeb, Feb 26, 2010.

  1. #1
    How do I make .getElementById() dynamic, as in the value inside () is a variable?
    My script looks like this:

    javascript code:

    mouseover(tag)
    {
       document.getElementById(tag).innerHTML = "Over";
    }
    mouseout(tag)
    {
       document.getElementById(tag).innerHTML = "Out";
    }
    Code (markup):
    html code:

    <div id="test" mouseOver = "mouseover(test)" mouseOut = "mouseout(test)">Text</div>
    <div id="test2" mouseOver = "mouseover(test2)" mouseOut = "mouseout(test2)">Text</div>
    Code (markup):
    Thanks.

    ~imzoeb
     
    Imozeb, Feb 26, 2010 IP
  2. rainborick

    rainborick Well-Known Member

    Messages:
    424
    Likes Received:
    33
    Best Answers:
    0
    Trophy Points:
    120
    #2
    Start by not using "mouseover" and "mouseout" as names for your own functions. They're reserved for JavaScript.
     
    rainborick, Feb 26, 2010 IP
  3. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Okay. What about my main question?
     
    Imozeb, Feb 26, 2010 IP
  4. wing

    wing Active Member

    Messages:
    210
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    58
    #4
    function hello(p_sTag) {
    	document.getElementById(p_sTag).innerHTML = 'Over';
    }
    
    function goodbye (p_sTag) {
    	document.getElementById(p_sTag).innerHTML = 'Out';
    }
    Code (markup):
    <div id="test" onmouseover="hello('test')" onmouseout="goodbye('test')">Text</div>
    
    <div id="test2" onmouseover="hello('test2')" onmouseout="goodbye('test2')">Text</div>
    HTML:
     
    wing, Feb 26, 2010 IP
  5. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #5
    no they are not.

    console.log(typeof(mouseover), typeof(mouseout));
    undefined undefined
    PHP:
    they are just event names that are fired, that's all.
     
    dimitar christoff, Feb 28, 2010 IP