reaching html elements

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chris

    reaching html elements

    I am trying to get at all the 'a' tags surrounded by a div tag with an
    id of 'id'

    This doesn't seem to do it, picks up all 'a' tags

    Code:
    links=document.getElementsByTagName(id).getElementsByTagName("a");
    Any thoughts?

    Thanks,

    Chris
  • Martin Honnen

    #2
    Re: reaching html elements

    Chris wrote:
    I am trying to get at all the 'a' tags surrounded by a div tag with an
    id of 'id'
    >
    This doesn't seem to do it, picks up all 'a' tags
    >
    Code:
    links=document.getElementsByTagName(id).getElementsByTagName("a");
    That should give an error as getElementsByTa gName gives you a node list
    and that node list does not have a getElementsByTa gName method.
    So you probably want
    Code:
    document.getElementById('id').getElementsByTagName('a')
    If that does not work for you as intended then you need to provide more
    details of the browser that does not work with.



    --

    Martin Honnen

    Comment

    • Chris

      #3
      Re: reaching html elements

      On 21 Jun, 11:49, Martin Honnen <mahotr...@yaho o.dewrote:
      Chris wrote:
      I am trying to get at all the 'a' tags surrounded by a div tag with an
      id of 'id'
      >
      This doesn't seem to do it, picks up all 'a' tags
      >
      Code:
      links=document.getElementsByTagName(id).getElementsByTagName("a");
      >
      That should give an error as getElementsByTa gName gives you a node list
      and that node list does not have a getElementsByTa gName method.
      So you probably want
      Code:
      document.getElementById('id').getElementsByTagName('a')
      If that does not work for you as intended then you need to provide more
      details of the browser that does not work with.
      >
      --
      >
      Martin Honnen
      http://JavaScript.FAQTs.com/
      Thanks Martin,

      I am working on a tooltip feature and my problem is that I can't
      figure out howto only apply the necessary javascript to relevant links
      and not every link on page. If I use

      document.getEle mentById('id'). getElementsByTa gName('a')

      this works for the first link I wrap in a tag with an id but not the
      rest e.g.

      Code:
      <div id="tooltipthis"><a href="" title="">link</a></div>
      <a href="" title="">No tooltip</a>
      <div id="tooltipthis"><a href="" title="">link</a></div>
      Thanks again,

      Chris

      Comment

      • Martin Honnen

        #4
        Re: reaching html elements

        Chris wrote:
        <div id="tooltipthis "><a href="" title="">link</a></div>
        <a href="" title="">No tooltip</a>
        <div id="tooltipthis "><a href="" title="">link</a></div>
        Ids need to be unique in the complete document so what you have above
        can't work.


        --

        Martin Honnen

        Comment

        • Chris

          #5
          Re: reaching html elements

          On 21 Jun, 12:37, Martin Honnen <mahotr...@yaho o.dewrote:
          Chris wrote:
          <div id="tooltipthis "><a href="" title="">link</a></div>
          <a href="" title="">No tooltip</a>
          <div id="tooltipthis "><a href="" title="">link</a></div>
          >
          Ids need to be unique in the complete document so what you have above
          can't work.
          >
          --
          >
          Martin Honnen
          http://JavaScript.FAQTs.com/
          Martin,

          Excuse my lack of javascript knowledge...I approach it like a bull in
          a china shop.

          After a break and some cheese and toast the obvious answer to my
          problem was to do getElementByNam e("tooltip") and work from there.

          Thanks again,

          Chris

          Comment

          • SAM

            #6
            Re: reaching html elements

            Chris a écrit :
            I am trying to get at all the 'a' tags surrounded by a div tag with an
            id of 'id'
            >
            This doesn't seem to do it, picks up all 'a' tags
            >
            links=document. getElementsByTa gName(id).getEl ementsByTagName ("a");
            >
            Any thoughts?
            links=document. getElementById( 'id').getElemen tsByTagName('A' );

            --
            sm

            Comment

            • Martin Honnen

              #7
              Re: reaching html elements

              Chris wrote:
              After a break and some cheese and toast the obvious answer to my
              problem was to do getElementByNam e("tooltip") and work from there.
              There is no method named 'getElementByNa me', only one named
              'getElementsByN ame' however div elements have no attribute named 'name'.



              --

              Martin Honnen

              Comment

              • SAM

                #8
                Re: reaching html elements

                Chris a écrit :
                >
                Code:
                document.getElementById('id').getElementsByTagName('a')
                >
                this works for the first link I wrap in a tag with an id but not the
                rest e.g.
                >
                Code:
                <div id="tooltipthis"><a href="" title="">link</a></div>
                <a href="" title="">No tooltip</a>
                <div id="tooltipthis"><a href="" title="">link</a></div>
                this html is wrong : ID can only be unique


                CSS :
                =====

                Code:
                div { height: 500px; margin: 20px; border: 1px solid }

                HTML :
                ======

                Code:
                <div id="tooltipthis_1">
                <a href="#tooltipthis_3" title="">link 1</a>
                </div>
                <a href="#" title="">No tooltip</a>
                <div id="tooltipthis_2">
                <a href="#" title="">link 2</a>
                </div>
                <div id="tooltipthis_3">
                <a href="#tooltipthis_1" title="">link 3</a>
                </div>
                JS :
                ====

                Code:
                function getLinks(commonId) {
                var d = document.getElementsByTagName('DIV');
                var L = [];
                for(var i=, S=d.length; i<S; i++)
                {
                if(S[i].id &&
                S[i].id.indexOf(commonId)>=0 &&
                S[i].getElementsByTagName('A') &&
                S[i].getElementsByTagName('A').length>0)
                {
                var A = S[i].getElementsByTagName('A');
                for(var k=0, Z=A.length; k<A; k++)
                {
                L[L.length] = A[k];
                }
                }
                }
                return L;
                }
                window.onload = function() { var T = getLinks('tooltipthis'); };

                HTML example of using :
                =============== ========

                Code:
                <button onclick="location = T[0].href;">
                click first link of tooltipthis
                </button>
                --
                sm

                Comment

                • SAM

                  #9
                  Re: reaching html elements

                  SAM a écrit :
                  >
                  function getLinks(common Id) {
                  var d = document.getEle mentsByTagName( 'DIV');
                  var L = [];
                  for(var i=, S=d.length; i<S; i++)

                  for(var i=0, S=d.length; i<S; i++)


                  --
                  sm

                  Comment

                  • Evertjan.

                    #10
                    Re: reaching html elements

                    Gregor Kofler wrote on 21 jun 2008 in comp.lang.javas cript:
                    for(var i = d.length; i--;) { ... };
                    or:

                    var i = d.length; while (i--) { ... };

                    or:

                    var i = 0; do { ... } while (d[++i]);


                    --
                    Evertjan.
                    The Netherlands.
                    (Please change the x'es to dots in my emailaddress)

                    Comment

                    • SAM

                      #11
                      Re: reaching html elements

                      Gregor Kofler a écrit :
                      SAM meinte:
                      >
                      >>function getLinks(common Id) {
                      >> var d = document.getEle mentsByTagName( 'DIV');
                      >> var L = [];
                      >> for(var i=, S=d.length; i<S; i++)
                      >>
                      >>
                      > for(var i=0, S=d.length; i<S; i++)
                      >
                      Why is S and L uppercase?
                      Why not ?
                      (easier for my glasses ?)

                      or I give upper case to collections, or I do it for the length,
                      I prefer like that instead of all in lower case.

                      for(var i = d.length; i--;)
                      >
                      is shorter and perhaps faster, too.
                      shorter? to write ? surely.
                      faster ? for JS ? would have to be equal, no ?

                      --
                      sm

                      Comment

                      • SAM

                        #12
                        Re: reaching html elements

                        Evertjan. a écrit :
                        Gregor Kofler wrote on 21 jun 2008 in comp.lang.javas cript:
                        >
                        >for(var i = d.length; i--;) { ... };
                        >
                        or:
                        >
                        var i = d.length; while (i--) { ... };
                        >
                        or:
                        >
                        var i = 0; do { ... } while (d[++i]);
                        Nothing more ? all was well seen about this ?

                        Each of you two doesn't have anything more to tell?

                        ie : a shorter way to get those @#?&! links.

                        --
                        sm

                        Comment

                        • Gregor Kofler

                          #13
                          Re: reaching html elements

                          SAM meinte:
                          >Why is S and L uppercase?
                          >
                          Why not ?
                          Uppercase variable names are supposed to indicate constructors.
                          or I give upper case to collections, or I do it for the length,
                          I prefer like that instead of all in lower case.
                          You can of course use whatever coding conventions you like. But since
                          you are *giving advices*, it would be smarter to follow best practices.
                          (Just changing cases for the sake of it, is a wacky concept anyway,
                          meseems...)

                          >for(var i = d.length; i--;)
                          >>
                          >is shorter and perhaps faster, too.
                          >
                          shorter? to write ? surely.
                          faster ? for JS ? would have to be equal, no ?
                          Faster. Yes. Since d.length has to evaluated only once.

                          Gregor


                          --
                          http://photo.gregorkofler.at ::: Landschafts- und Reisefotografie
                          http://web.gregorkofler.com ::: meine JS-Spielwiese
                          http://www.image2d.com ::: Bildagentur für den alpinen Raum

                          Comment

                          • SAM

                            #14
                            Re: reaching html elements

                            Gregor Kofler a écrit :
                            SAM meinte:
                            >
                            >>for(var i = d.length; i--;)
                            >>>
                            >>is shorter and perhaps faster, too.
                            >>
                            >shorter? to write ? surely.
                            >faster ? for JS ? would have to be equal, no ?
                            >
                            Faster. Yes. Since d.length has to evaluated only once.
                            with :
                            for(var i=0, L=d.length; i<L; i++)
                            L is evaluated several times ?

                            --
                            sm

                            Comment

                            • Gregor Kofler

                              #15
                              Re: reaching html elements

                              SAM meinte:
                              with :
                              for(var i=0, L=d.length; i<L; i++)
                              L is evaluated several times ?
                              Sorry, my wrong - in this case it's evaluated only once (and therefore
                              just as fast as the alternative versions).

                              Gregor



                              --
                              http://photo.gregorkofler.at ::: Landschafts- und Reisefotografie
                              http://web.gregorkofler.com ::: meine JS-Spielwiese
                              http://www.image2d.com ::: Bildagentur für den alpinen Raum

                              Comment

                              Working...