How do I dynamicly get the id of an element?

Discussion in 'JavaScript' started by Imozeb, Apr 12, 2010.

  1. #1
    I want to get the id of this element so I can use it. How would I do it? (I want the <a> to have no id yet still be able to be called from javascript)

    Example:

    Javascript
    
    function imgover(name)
    {
      document.getElementById(name).style.cursor = 'pointer';
    }
    
    Code (markup):
    
    <a onmouseover="imgover('this')">link here</a>
    
    HTML:
    Is this even possible?

    Thanks.

    ~imozeb
     
    Imozeb, Apr 12, 2010 IP
  2. rainborick

    rainborick Well-Known Member

    Messages:
    424
    Likes Received:
    33
    Best Answers:
    0
    Trophy Points:
    120
    #2
    You can check the results of getElementsByTagName and locate the one you want.
     
    rainborick, Apr 12, 2010 IP
  3. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    But there could be any number of <a> tags on my page. Is there anyway I can pass along a referer in the function call?
     
    Imozeb, Apr 12, 2010 IP
  4. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #4
    you can of course but not 'this'

    
    // <a onmouseover="imgover(this);">link here</a> // using this as scope for function, no " "
    
    var imgover = function(el) {
        if (!el) return; // needs to be an object, in our case the callee = a
        el.style.cursor = 'pointer'; // for ie this needs to be el.style.cursor = 'hand'; iirc
    };
    
    Code (javascript):
    however, you don't need to do the cursor stuff, just add a href as # and it will automatically do the cursor for you.
     
    dimitar christoff, Apr 13, 2010 IP
  5. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks. Using # worked.
     
    Imozeb, Apr 13, 2010 IP