How to use getElementById with a JS variable?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • luftikus143
    New Member
    • Jan 2007
    • 97

    How to use getElementById with a JS variable?

    Hi there,

    I am fighting with a small, probably dumb, issue.

    I want to call the properties of an SVG element. The element is composed of several elements:

    Code:
    t = 'id_' + selectedIndex + '_' + clicked_element_year;

    Now, it seems I can't call it this way:

    Code:
    clicked_element_text = svgDocument.getElementById(t).getAttributeNS(null,'mouseovertext');

    I guess that, as the ID is textual and not numerical, it should somehow be within apostrophes, as it works this way:

    Code:
    clicked_element_text = svgDocument.getElementById('id_784_2008').getAttributeNS(null,'mouseovertext');

    But how would the Javascript need to look like in order to be accepted? I tried it with multiple apostrophes, and with backslash-apostrophe, but in vain.

    Thanks for any hints!
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    that shoudn't matter whether you pass the string as literal or as variable. besides that, IDs are never numerical (matter of definition).

    did you verify that the passed parameter is correct?

    Comment

    • luftikus143
      New Member
      • Jan 2007
      • 97

      #3
      Yep, I tried to verify this a couple of times and via different steps.

      But what is really strange, is now the following:

      When I use an alert:
      Code:
      alert(svgDocument.getElementById(t).getAttributeNS(null,'mouseovertext'));

      it gives me what I want. But when using just to pass that to a variable, it doesn't work:
      Code:
      clicked_element_text = svgDocument.getElementById(t).getAttributeNS(null,'mouseovertext');
      Why would that be?

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        But when using just to pass that to a variable, it doesn't work:
        please explain "doesn't work".

        Comment

        • luftikus143
          New Member
          • Jan 2007
          • 97

          #5
          Sorry for the imprecision.

          The error message I get (in Safari, Error Console) is:
          Code:
          TypeError: 'null' is not an object (evaluating 'svgDocument.getElementById(t).getAttributeNS')

          Comment

          • luftikus143
            New Member
            • Jan 2007
            • 97

            #6
            Ah gush, as usual, tiny thing. The name for the element I tried to call was too complex for me to see that there was one letter too much - the one letter I did indeed need for another element to call. Sorry!

            Comment

            Working...