tag id name

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

    tag id name

    I have several <div id='DivNumber1' > on my page.
    the Id can be a random number between 1 and 65535.

    I need a script where I can get all div tags and check if the ID is
    DivNumber.

    This does not work:
    if(document.get ElementById)
    {
    var ar = document.getEle mentsByTagName( "DIV");
    for (var i=0; i<ar.length; i++)
    {
    s1 = new String(ar[i].id);
    if(s1.search('G roupItem')>=0) ar[i].style.display = "none";
    }
    }

  • GTi

    #2
    Re: tag id name

    Whops: Bugfix:

    if(document.get ElementById)
    {
    var ar = document.getEle mentsByTagName( "DIV");
    for (var i=0; i<ar.length; i++)
    {
    s1 = new String(ar[i].id);
    if(s1.search('D ivNumber')>=0) ar[i].style.display = "none";
    }
    }

    Comment

    • Evertjan.

      #3
      Re: tag id name

      GTi wrote on 13 dec 2005 in comp.lang.javas cript:
      [color=blue]
      > Whops: Bugfix:
      >
      > if(document.get ElementById)
      > {
      > var ar = document.getEle mentsByTagName( "DIV");
      > for (var i=0; i<ar.length; i++)
      > {
      > s1 = new String(ar[i].id);
      > if(s1.search('D ivNumber')>=0) ar[i].style.display = "none";
      > }
      > }[/color]

      Simplified:

      if(document.get ElementsByTagNa me) {
      var ar = document.getEle mentsByTagName( "DIV");
      for (var i=0; i<ar.length; i++)
      if (/DivNumber/.test(ar[i].id))
      ar[i].style.display = "none";
      }



      --
      Evertjan.
      The Netherlands.
      (Replace all crosses with dots in my emailaddress)

      Comment

      • GTi

        #4
        Re: tag id name

        Evertjan,

        what does / is syntax for:
        /DivNumber/.test()
        in
        if (/DivNumber/.test(ar[i].id))
        ???

        Comment

        • Evertjan.

          #5
          Re: tag id name

          GTi wrote on 13 dec 2005 in comp.lang.javas cript:
          [color=blue]
          > Evertjan,
          >
          > what does / is syntax for:
          > /DivNumber/.test()
          > in
          > if (/DivNumber/.test(ar[i].id))
          >[/color]

          /DivNumber/ is a regular expression.
          regex test tests the string and returns true or false.
          You don't need to setup an object here.

          --
          Evertjan.
          The Netherlands.
          (Replace all crosses with dots in my emailaddress)

          Comment

          • Thomas 'PointedEars' Lahn

            #6
            Re: tag id name

            GTi wrote:
            [color=blue]
            > Whops: Bugfix:[/color]

            Not really.
            [color=blue]
            > if(document.get ElementById)
            > {
            > var ar = document.getEle mentsByTagName( "DIV");[/color]

            Although both methods are defined in the same DOM Level, support
            for Document::getEl ementById() does not imply support for
            Document::getEl ementsByTagName () in an implementation, so you
            should also feature-test the latter on run-time.

            <URL:http://PointedEars.de/scripts/test/whatami>, ยง 2.
            [color=blue]
            > for (var i=0; i<ar.length; i++)[/color]

            Instead

            for (var i = ar.length; i--;)

            since order does not matter; if it does matter, use

            for (var i = 0, len = ar.length; i < len; i++)
            [color=blue]
            > {
            > s1 = new String(ar[i].id);[/color]

            Java != JavaScript. You do not need to create a String object in order
            to work with string values or to apply properties of String.prototyp e.
            For type conversion to string, use the String(noString ) function or
            the toString(noStri ng).method. And since the value of the `id'
            attribute/property already is a string, there is no type conversion
            necessary.
            [color=blue]
            > if (s1.search('Div Number')>=0) ar[i].style.display = "none";[/color]

            Instead:

            var o = ar[i];
            if (o.id.indexOf(' DivNumber') >= 0)
            {
            if (typeof o.style != "undefined"
            && typeof o.style.display != "undefined" )
            {
            o.style.display = "none";
            }
            }
            [color=blue]
            > }[/color]


            HTH

            PointedEars

            Comment

            • GTi

              #7
              Re: tag id name

              I have a menu structure with several <div id=DivNumberxxx x> that is
              blocks of menu groups. But I also have other <div id=whatever> sections
              that I don't want to hide.
              But it still don't work - it hide <div id=whatever> also :

              I have this function:

              function MainMenuGroupEx pand(obj)
              {
              if(document.get ElementsByTagNa me)
              {
              var el = document.getEle mentById(obj);
              var ar =
              document.getEle mentById("MenuG roup").getEleme ntsByTagName("D IV");
              if(el.style.dis play == "none")
              {
              for (var i=ar.length; i--;)
              {
              var o = ar[i];
              if (o.id.indexOf(' DivNumber') == 0)
              {
              if((typeof o.style != "undefined" ) && (typeof o.style.display
              != "undefined" ))
              {
              o.style.display = "none";
              }
              }
              }
              el.style.displa y = "block";
              }
              }
              }

              Comment

              • GTi

                #8
                Re: tag id name

                Yes - it does work.
                It was a spelling error by me.
                MenuGroup -> MenyGroup
                Sorry and thanks for helping me :)

                Comment

                • Thomas 'PointedEars' Lahn

                  #9
                  Re: tag id name

                  GTi wrote:
                  [color=blue]
                  > I have a menu structure with several <div id=DivNumberxxx x> that is
                  > blocks of menu groups. But I also have other <div id=whatever> sections
                  > that I don't want to hide.
                  > But it still don't work - it hide <div id=whatever> also :
                  >
                  > I have this function:
                  >
                  > function MainMenuGroupEx pand(obj)
                  > {
                  > if(document.get ElementsByTagNa me)[/color]
                  ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^[color=blue]
                  > {
                  > var el = document.getEle mentById(obj);[/color]
                  ^^^^^^^^^^^^^^^ ^^^^^^^^^[color=blue]
                  > var ar =
                  > document.getEle mentById("MenuG roup").getEleme ntsByTagName("D IV");[/color]
                  ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^[color=blue]
                  > if(el.style.dis play == "none")
                  > {
                  > for (var i=ar.length; i--;)
                  > {
                  > var o = ar[i];
                  > if (o.id.indexOf(' DivNumber') == 0)[/color]
                  ^^
                  Have you /really/ read _and_ understood what I posted?
                  [color=blue]
                  > [...][/color]


                  PointedEars

                  Comment

                  • GTi

                    #10
                    Re: tag id name

                    Hmm...
                    Looks like I have overlooked something here.
                    Yes I have readed it but I admit that I do not understand it 100%
                    [color=blue]
                    > if (o.id.indexOf(' DivNumber') == 0)[/color]
                    It is always the beginning of the tag name. It will ignore
                    id='otherDivNum er'

                    I must try to understand what the other 'red markers' are.

                    Comment

                    • Thomas 'PointedEars' Lahn

                      #11
                      Re: tag id name

                      GTi wrote:
                      [color=blue][color=green]
                      > > if (o.id.indexOf(' DivNumber') == 0)[/color]
                      > It is always the beginning of the tag name.[/color]

                      You mean the attribute value.
                      [color=blue]
                      > It will ignore id='otherDivNum er'[/color]

                      Then this line is OK so.
                      [color=blue]
                      > I must try to understand what the other 'red markers' are.[/color]

                      In case of questions, do not hesitate to ask.

                      But please learn how to quote on Usenet before:
                      <URL:http://jibbering.com/faq/faq_notes/pots1.html#ps1P ost>


                      PointedEars

                      Comment

                      • Jasen Betts

                        #12
                        Re: tag id name

                        On 2005-12-13, GTi <tunlid@gmail.c om> wrote:[color=blue]
                        > I have several <div id='DivNumber1' > on my page.
                        > the Id can be a random number between 1 and 65535.
                        >
                        > I need a script where I can get all div tags and check if the ID is
                        > DivNumber.
                        >
                        > var ar = document.getEle mentsByTagName( "DIV");[/color]

                        how about this?

                        var ar = document.getEle mentsByTagName( "div");



                        --

                        Bye.
                        Jasen

                        Comment

                        Working...