referencing a <tr> object

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

    #1

    referencing a <tr> object

    Hi,

    if i have this code: <form><input type=text name="id1"></form> then with
    javascript i can reference this with this.form.id1, how can i reference
    something like this:

    <table>
    <tr name="id2">
    ..
    ..
    </table>

    ?

    this.id2 doesn't work

    thank you,
    Greg
  • Michael Winter

    #2
    Re: referencing a &lt;tr&gt; object

    Gregor Rot wrote:
    [color=blue]
    > if i have this code: <form><input type=text name="id1"></form> then with
    > javascript i can reference this with this.form.id1 [...][/color]

    That works because form controls with name and id attributes are usually
    made available as properties of a FORM element. However, I think it's
    preferable to use the elements collection to reference form controls:

    this.form.eleme nts.id1

    The shorthand form was, in my opinion, a mistake and the elements
    collection is standardised.
    [color=blue]
    > how can i reference something like this:
    >
    > <table>
    > <tr name="id2">[/color]

    You shouldn't be able to. Table rows, like the vast majority of
    elements, do not have name attributes. They do have id attributes,
    though. You can then use the getElementById method to obtain a reference:

    <tr id="id2">


    var row = null;

    if(document.get ElementById) {
    row = document.getEle mentById('id2') ;
    }
    if(row) {
    /* Now have a reference to the table row. */
    }

    [snip]

    Hope that helps,
    Mike

    --
    Michael Winter
    Replace ".invalid" with ".uk" to reply by e-mail.

    Comment

    • Gregor Rot

      #3
      Re: referencing a &lt;tr&gt; object

      Michael Winter wrote:[color=blue]
      > Gregor Rot wrote:
      >[color=green]
      >> if i have this code: <form><input type=text name="id1"></form> then
      >> with javascript i can reference this with this.form.id1 [...][/color]
      >
      >
      > That works because form controls with name and id attributes are usually
      > made available as properties of a FORM element. However, I think it's
      > preferable to use the elements collection to reference form controls:
      >
      > this.form.eleme nts.id1
      >
      > The shorthand form was, in my opinion, a mistake and the elements
      > collection is standardised.
      >[color=green]
      >> how can i reference something like this:
      >>
      >> <table>
      >> <tr name="id2">[/color]
      >
      >
      > You shouldn't be able to. Table rows, like the vast majority of
      > elements, do not have name attributes. They do have id attributes,
      > though. You can then use the getElementById method to obtain a reference:
      >
      > <tr id="id2">
      >
      >
      > var row = null;
      >
      > if(document.get ElementById) {
      > row = document.getEle mentById('id2') ;
      > }
      > if(row) {
      > /* Now have a reference to the table row. */
      > }
      >
      > [snip]
      >
      > Hope that helps,
      > Mike
      >[/color]
      Thank you, it works nicely.

      by,
      Greg

      Comment

      • Gregor Rot

        #4
        Re: referencing a &lt;tr&gt; object

        Gregor Rot wrote:[color=blue]
        > Michael Winter wrote:
        >[color=green]
        >> Gregor Rot wrote:
        >>[color=darkred]
        >>> if i have this code: <form><input type=text name="id1"></form> then
        >>> with javascript i can reference this with this.form.id1 [...][/color]
        >>
        >>
        >>
        >> That works because form controls with name and id attributes are
        >> usually made available as properties of a FORM element. However, I
        >> think it's preferable to use the elements collection to reference form
        >> controls:
        >>
        >> this.form.eleme nts.id1
        >>
        >> The shorthand form was, in my opinion, a mistake and the elements
        >> collection is standardised.
        >>[color=darkred]
        >>> how can i reference something like this:
        >>>
        >>> <table>
        >>> <tr name="id2">[/color]
        >>
        >>
        >>
        >> You shouldn't be able to. Table rows, like the vast majority of
        >> elements, do not have name attributes. They do have id attributes,
        >> though. You can then use the getElementById method to obtain a reference:
        >>
        >> <tr id="id2">
        >>
        >>
        >> var row = null;
        >>
        >> if(document.get ElementById) {
        >> row = document.getEle mentById('id2') ;
        >> }
        >> if(row) {
        >> /* Now have a reference to the table row. */
        >> }
        >>
        >> [snip]
        >>
        >> Hope that helps,
        >> Mike
        >>[/color]
        > Thank you, it works nicely.
        >
        > by,
        > Greg[/color]

        The problem is that in Mozilla the rows when appearing/disappearing
        display strangely, and making more and more space ... strange. Any
        experience with that? (in IE it works fine)

        I am attaching you the full code so somebody can check - the text at the
        end of the page when sqitching the scrollbar gets more and more pushed
        down the page...

        tnx,
        Greg

        <html>
        <head>
        <script language="JavaS cript" type="text/javascript">
        function getOther(sel,fl d){
        if (sel.selectedIn dex==sel.option s.length-1) fld.disabled=fa lse; else
        fld.disabled=tr ue;
        var row1 = null;
        var row2 = null;
        var row3 = null;

        if(document.get ElementById) {
        row1 = document.getEle mentById('id1') ;
        row2 = document.getEle mentById('id2') ;
        row3 = document.getEle mentById('id3') ;
        }
        if(row1) {
        row1.style.disp lay =
        (sel.selectedIn dex==sel.option s.length-1)?"inline":"no ne";
        }
        if(row2) {
        row2.style.disp lay =
        (sel.selectedIn dex==sel.option s.length-1)?"inline":"no ne";
        }
        if(row3) {
        row3.style.disp lay =
        (sel.selectedIn dex==sel.option s.length-1)?"inline":"no ne";
        }
        }
        </script>
        </head>

        <body>
        <form action="">
        <div align="center">
        <table border="0" name="tb" id="tb" width="364">
        <tr>
        <td width="177"><b> <font face="Verdana" size="2">Podatk i o
        plačniku</font></b></td>
        <td width="177"><fo nt face="Verdana"> <select size="1" name="D1"
        onchange="getOt her(this,this.f orm.naziv);getO ther(this,this. form.sedez);get Other(this,this .form.davcna);" >
        <option selected value="sam">sam oplačnik</option>
        <option value="podjetje ">podjetje</option>
        </select></font></td>
        </tr>
        <tr id="id1" style="display: none;">
        <td width="177">Naz iv:</td>
        <td width="177"><fo nt face="Verdana">
        <input type="text" name="naziv" size="20" disabled></font></td>
        </tr>
        <tr id="id2" style="display: none;">
        <td width="177">Sed e<span lang="sl">ž</span></td>
        <td width="177"><fo nt face="Verdana">
        <input type="text" name="sedez" size="20" disabled></font></td>
        </tr>
        <tr id="id3" style="display: none;">
        <td width="177">Dav čna številka</td>
        <td width="177"><fo nt face="Verdana">
        <input type="text" name="davcna" size="20" disabled></font></td>
        </tr>
        </table>
        </div>
        <p align="center"> some other text</p>
        </form>
        </body>

        </html>

        Comment

        • Dietmar Meier

          #5
          Re: referencing a &lt;tr&gt; object

          Gregor Rot wrote:
          [color=blue]
          > The problem is that in Mozilla the rows when appearing/disappearing
          > display strangely, and making more and more space ... strange. Any
          > experience with that? (in IE it works fine)[/color]

          Assign "", not "inline", to the display property to fix that.

          Some more changes I suggest for your script:

          function getOther(sel, fld) {
          var lastOpt = sel.selectedInd ex == sel.options.len gth - 1;
          fld.disabled = !lastOpt;
          if (document.getEl ementById) {
          var rows = [
          document.getEle mentById('id1') ,
          document.getEle mentById('id2') ,
          document.getEle mentById('id3')
          ];
          var disp = lastOpt? "" : "none";
          for (var i=0; i<rows.length; i++) {
          if (rows[i] && rows[i].style) {
          rows[i].style.display = disp;
          }
          }
          }
          }

          ciao, dhgm

          Comment

          • Gregor Rot

            #6
            Re: referencing a &lt;tr&gt; object

            Dietmar Meier wrote:[color=blue]
            > Gregor Rot wrote:
            >[color=green]
            >> The problem is that in Mozilla the rows when appearing/disappearing
            >> display strangely, and making more and more space ... strange. Any
            >> experience with that? (in IE it works fine)[/color]
            >
            >
            > Assign "", not "inline", to the display property to fix that.
            >
            > Some more changes I suggest for your script:
            >
            > function getOther(sel, fld) {
            > var lastOpt = sel.selectedInd ex == sel.options.len gth - 1;
            > fld.disabled = !lastOpt;
            > if (document.getEl ementById) {
            > var rows = [
            > document.getEle mentById('id1') ,
            > document.getEle mentById('id2') ,
            > document.getEle mentById('id3')
            > ];
            > var disp = lastOpt? "" : "none";
            > for (var i=0; i<rows.length; i++) {
            > if (rows[i] && rows[i].style) {
            > rows[i].style.display = disp;
            > }
            > }
            > }
            > }
            >
            > ciao, dhgm[/color]
            TNX, it works fine now.

            Greg

            Comment

            Working...