Script help

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

    Script help

    after Evertjan advice ;), I'm trying to cut down a complex task in many
    easier ones.

    Here is the code I'm trying to get to work:

    function CheckGroup(X){
    code = X.value;
    alert(code); //this is OK !!!!

    obj = document.getEle mentById('optio n[]');
    alert('object length '+obj.length);
    for ( index = 0; index < obj.length; index++ )
    {
    testcode = obj[index].value;
    if(code == testcode)
    alert('found... ');
    }
    }

    then I print several input type like this:
    //generated by PHP...$newvalue are new values at every checkbox (concat in
    PHP is ".")
    <input type="checkbox" name="option[]" value=".$newval ue."
    onclick="CheckG roup(this)");>

    Every checkbox is part of the option array (look at the name). In the
    CheckGroup function, I try to retrieve all $newvalue from the option[]
    array. So I try to retrieve every single item in the option[] array, but
    unfortunately, it doesn't work.
    alert(obj.lengt h); show and undefined value.

    How can I retrieve such items values ???



  • Evertjan.

    #2
    Re: Script help

    Bob Bedford wrote on 01 jul 2004 in comp.lang.javas cript:
    [color=blue]
    > after Evertjan advice ;), I'm trying to cut down a complex task in
    > many easier ones.
    >
    > Here is the code I'm trying to get to work:
    >
    > function CheckGroup(X){
    > code = X.value;
    > alert(code); //this is OK !!!!
    >
    > obj = document.getEle mentById('optio n[]');
    > alert('object length '+obj.length);
    > for ( index = 0; index < obj.length; index++ )
    > {
    > testcode = obj[index].value;
    > if(code == testcode)
    > alert('found... ');
    > }
    >}
    >
    > then I print several input type like this:
    > //generated by PHP...$newvalue are new values at every checkbox
    > (concat in PHP is ".")
    > <input type="checkbox" name="option[]" value=".$newval ue."[/color]

    name="option[]"

    1 This is not a HTML legal name.
    2 this is not an ID, so getElementById should not find it [exept that som
    browsers might have a bug.
    3 id ="option[]" would aldso be illegal HTML.
    4 you cannot make a javascript array like this

    [color=blue]
    > onclick="CheckG roup(this)");>[/color]

    the ); is illegal
    [color=blue]
    >
    > Every checkbox is part of the option array (look at the name). In the
    > CheckGroup function, I try to retrieve all $newvalue from the option[]
    > array. So I try to retrieve every single item in the option[] array,
    > but unfortunately, it doesn't work.
    > alert(obj.lengt h); show and undefined value.
    >
    > How can I retrieve such items values ???[/color]

    I can only exchange PHP [because of my lack of knowledge of PHP]
    by ASP(!!!) and you would get something like this:

    <% for n= 1 to 25 %>
    <input type="checkbox"
    name="<%=option (n)%>"
    id="<%=option(n )%>"
    onclick="CheckG roup(this);"
    value="<%=newva lue(n)%>">
    <br>
    <% next %>

    And the clientside javascript can do:

    function CheckGroup(X){
    id = X.value;
    alert(id); //this is OK !!!
    }

    and

    obj = document.getEle mentById('<%=op tion(7)%>');
    alert(obj.value )

    ==============

    Bob, perhaps we will get somewhere.


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

    Comment

    • Randy Webb

      #3
      Re: Script help

      Evertjan. wrote:
      [color=blue]
      ><input type="checkbox" name="option[]" value=".$newval ue."
      >
      >
      > name="option[]"
      >
      > 1 This is not a HTML legal name.[/color]

      Sure it is.
      [color=blue]
      > 2 this is not an ID, so getElementById should not find it [exept that som
      > browsers might have a bug.
      > 3 id ="option[]" would aldso be illegal HTML.[/color]

      That is true, but as a name attribute, its perfectly legal and valid.



      <quote>
      These characters are illegal within ID attributes in the standard
      (x)HTML doctypes and javascript Identifiers
      </quote>

      Illegal in ID's, perfectly valid in NAMES.
      --
      Randy
      Chance Favors The Prepared Mind
      comp.lang.javas cript FAQ - http://jibbering.com/faq/

      Comment

      • Martin Honnen

        #4
        Re: Script help



        Bob Bedford wrote:

        [color=blue]
        > Here is the code I'm trying to get to work:
        >
        > function CheckGroup(X){
        > code = X.value;
        > alert(code); //this is OK !!!!
        >
        > obj = document.getEle mentById('optio n[]');
        > alert('object length '+obj.length);
        > for ( index = 0; index < obj.length; index++ )
        > {
        > testcode = obj[index].value;
        > if(code == testcode)
        > alert('found... ');
        > }
        > }
        >
        > then I print several input type like this:
        > //generated by PHP...$newvalue are new values at every checkbox (concat in
        > PHP is ".")
        > <input type="checkbox" name="option[]" value=".$newval ue."
        > onclick="CheckG roup(this)");>[/color]

        Don't post your PHP here, we deal with HTML and JavaScript so use
        view-source in your browser so that you can post HTML/JavaScript as the
        browser sees it and not any server side scripting.

        Now to your code, you pass the <input> element object to the CheckGroup
        function which is fine but then you try
        document.getEle mentById('optio n[]');
        which is nonsense as you do not have any element with that id, what you
        have are elements with that name. What you are looking for is
        var inputs = X.form.elements[X.name];
        This will give you a collection of all the controls in the form that
        have the same name as the input you pass in as X.

        As long as you are in the realms of form scripting the JavaScript 1.3
        client-side documentation applies, I suggest you have a look at


        which deals with that

        --

        Martin Honnen


        Comment

        • Bob Bedford

          #5
          Re: Script help

          "Evertjan." <exjxw.hannivoo rt@interxnl.net > a écrit dans le message de
          news:Xns9519BE8 9E4725eejj99@19 4.109.133.29...[color=blue]
          > Bob Bedford wrote on 01 jul 2004 in comp.lang.javas cript:
          >[color=green]
          > > after Evertjan advice ;), I'm trying to cut down a complex task in
          > > many easier ones.
          > >
          > > Here is the code I'm trying to get to work:
          > >
          > > function CheckGroup(X){
          > > code = X.value;
          > > alert(code); //this is OK !!!!
          > >
          > > obj = document.getEle mentById('optio n[]');
          > > alert('object length '+obj.length);
          > > for ( index = 0; index < obj.length; index++ )
          > > {
          > > testcode = obj[index].value;
          > > if(code == testcode)
          > > alert('found... ');
          > > }
          > >}
          > >
          > > then I print several input type like this:
          > > //generated by PHP...$newvalue are new values at every checkbox
          > > (concat in PHP is ".")
          > > <input type="checkbox" name="option[]" value=".$newval ue."[/color]
          >
          > name="option[]"
          >
          > 1 This is not a HTML legal name.
          > 2 this is not an ID, so getElementById should not find it [exept that som
          > browsers might have a bug.
          > 3 id ="option[]" would aldso be illegal HTML.
          > 4 you cannot make a javascript array like this
          >
          >[color=green]
          > > onclick="CheckG roup(this)");>[/color]
          >
          > the ); is illegal
          >[color=green]
          > >
          > > Every checkbox is part of the option array (look at the name). In the
          > > CheckGroup function, I try to retrieve all $newvalue from the option[]
          > > array. So I try to retrieve every single item in the option[] array,
          > > but unfortunately, it doesn't work.
          > > alert(obj.lengt h); show and undefined value.
          > >
          > > How can I retrieve such items values ???[/color]
          >
          > I can only exchange PHP [because of my lack of knowledge of PHP]
          > by ASP(!!!) and you would get something like this:
          >
          > <% for n= 1 to 25 %>
          > <input type="checkbox"
          > name="<%=option (n)%>"
          > id="<%=option(n )%>"
          > onclick="CheckG roup(this);"
          > value="<%=newva lue(n)%>">
          > <br>
          > <% next %>
          >
          > And the clientside javascript can do:
          >
          > function CheckGroup(X){
          > id = X.value;
          > alert(id); //this is OK !!!
          > }
          >
          > and
          >
          > obj = document.getEle mentById('<%=op tion(7)%>');
          > alert(obj.value )
          >
          > ==============
          >
          > Bob, perhaps we will get somewhere.[/color]

          Finally, with your help, I got it to work:
          Javascript:
          function CheckGroup(X){
          code = X.value;
          alert(code);
          obj = document.Submit Form.option;
          for ( index = 0; index < obj.length; index++ ){
          alert('object values '+obj[index].value);
          }
          }

          HMTL code:
          ....
          <input type="checkbox" name="option" value="1422862"
          onclick="CheckG roup(this)">
          <input type="checkbox" name="option" value="1422863"
          onclick="CheckG roup(this)">
          ....

          Thanks for your help !!!

          Now I will try to store a list of exclusion with this object. If somebody
          know how to do so, please let me know.

          Regards



          Comment

          • Bob Bedford

            #6
            Re: Script help

            Hi Evertjan,

            Finally, I've been able to do what I wanted in the original post.

            Thanks for your help !

            (if you are interested, let me know....)

            Best regards.

            BoB



            Comment

            • Thomas 'PointedEars' Lahn

              #7
              Re: Script help

              Randy Webb wrote:[color=blue]
              > Evertjan. wrote:[color=green]
              >> 2 this is not an ID, so getElementById should not find it [exept
              >> that som browsers might have a bug.
              >> 3 id ="option[]" would aldso be illegal HTML.[/color]
              >
              > That is true, but as a name attribute, its perfectly legal and valid.
              >
              > http://www.jibbering.com/faq/#FAQ4_25
              >
              > <quote>
              > These characters are illegal within ID attributes in the standard
              > (x)HTML doctypes and javascript Identifiers
              > </quote>[/color]

              I think this FAQ entry has created enough
              misunderstandin gs to be rewritten.
              [color=blue]
              > Illegal in ID's, perfectly valid in NAMES.[/color]

              Not *all* NAMEs because the "name" attributes you
              are referring to are of type CDATA:

              <http://www.w3.org/TR/html4/types.html#h-6.2>
              <http://www.w3.org/TR/html4/index/attributes.html >


              PointedEars

              Comment

              • Randy Webb

                #8
                Re: Script help

                Thomas 'PointedEars' Lahn wrote:
                [color=blue]
                > Randy Webb wrote:
                >[color=green]
                >>Evertjan. wrote:
                >>[color=darkred]
                >>>2 this is not an ID, so getElementById should not find it [exept
                >>>that som browsers might have a bug.
                >>>3 id ="option[]" would aldso be illegal HTML.[/color]
                >>
                >>That is true, but as a name attribute, its perfectly legal and valid.
                >>
                >>http://www.jibbering.com/faq/#FAQ4_25
                >>
                >><quote>
                >>These characters are illegal within ID attributes in the standard
                >>(x)HTML doctypes and javascript Identifiers
                >></quote>[/color]
                >
                >
                > I think this FAQ entry has created enough
                > misunderstandin gs to be rewritten.[/color]

                But it is factually correct as written, and the current wording was
                chosen for a very explicit reason.




                --
                Randy
                Chance Favors The Prepared Mind
                comp.lang.javas cript FAQ - http://jibbering.com/faq/

                Comment

                Working...