find form elements in particular TableRow

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • thomasamillergoogle@yahoo.com

    find form elements in particular TableRow

    Hi, As you can see from the code below I have a simple js function
    called getFormElements inTableRow(rowN ame). rowName is the ID of the
    tableRow. I just want to use js to find the child ID's of all the form
    elements in that particular tableRow. What am I doing wrong?


    <table><tr id="where12" style="DISPLAY: none">
    <td><select name="parameter 12" id="parameter12 ">
    <option value="1">casen umber</option>
    <option value="2">analy st</option>
    </select>
    <div class='button'>
    <A href='#' onclick="javasc ript:getFormEle mentsinTableRow
    ('where12');"> DELETE ROW </A>
    <div></td></tr></table>

    <Script jang=javascript >
    function getFormElements inTableRow(rowN ame){

    var nodeList = document.getEle mentById(rowNam e);

    for (var i = 0; i < nodeList.length ; i++)
    {
    var node = nodeList[i];
    alert(node.id);
    }

    }
    <script>

  • RobG

    #2
    Re: find form elements in particular TableRow

    thomasamillergo ogle@yahoo.com wrote:[color=blue]
    > Hi, As you can see from the code below I have a simple js function
    > called getFormElements inTableRow(rowN ame). rowName is the ID of the
    > tableRow. I just want to use js to find the child ID's of all the form
    > elements in that particular tableRow. What am I doing wrong?
    >
    >
    > <table><tr id="where12" style="DISPLAY: none">
    > <td><select name="parameter 12" id="parameter12 ">
    > <option value="1">casen umber</option>
    > <option value="2">analy st</option>
    > </select>
    > <div class='button'>
    > <A href='#' onclick="javasc ript:getFormEle mentsinTableRow
    > ('where12');"> DELETE ROW </A>[/color]

    There is no need for the javascript pseudo-protocol, and your onclick
    should return false to stop browsers from going back to the top of the
    page.

    <A href='#' onclick="
    getFormElements inTableRow('whe re12');
    return false;
    "> DELETE ROW </A>

    Even better would be to not subvert an A element and use something more
    suitable - span, div, input type=button, button, etc.
    [color=blue]
    > <div></td></tr></table>[/color]

    ^-- Closing tag?

    </div>...
    [color=blue]
    >
    > <Script jang=javascript >[/color]

    I'll assume that is a copy/paste error. The 'lang' attribute never
    existed for script elements, there is a depreciated 'language'
    attribute, but type is required:

    <script type="text/javascript">
    [color=blue]
    > function getFormElements inTableRow(rowN ame){
    >
    > var nodeList = document.getEle mentById(rowNam e);[/color]

    getElementById( ) returns a reference to an element. In this case,
    'nodeList' will be a reference to the TR element - it is not an array
    or collection. nodeList.length will be 'undefined'.

    Using what are (to me) more suitable variable names:

    function getFormElements inTableRow( rowID ){
    var r = document.getEle mentById(rowID) ;
    var rA = r.getElementsBy TagName('*');
    [color=blue]
    >
    > for (var i = 0; i < nodeList.length ; i++)
    > {
    > var node = nodeList[i];
    > alert(node.id);
    > }[/color]

    Here is an alternative that shows the elements and their ID if they
    have one:

    var x, i=0;
    while ( x = rA[i++] ) {
    alert( x.nodeName + '\n' +
    ((x.id)? 'id: ' + x.id : ' no ID'));
    }[color=blue]
    >
    > }
    > <script>[/color]

    Closing tag?

    </script>[color=blue]
    >[/color]

    If you want just the form elements, you'll have to sift through the
    collection in rA. If this is for more than casual interest, you should
    feature test getElementById and getElementsByTa gName before using
    them and you may also want to add support for document.all.

    Here's the full script:

    <script type="text/javascript">

    function getFormElements inTableRow( rowID ){
    var r = document.getEle mentById( rowID )
    var rA = r.getElementsBy TagName('*');
    var x, i=0;

    while ( x = rA[i++] ) {
    alert( x.nodeName + '\n' +
    ((x.id)? 'id: ' + x.id : 'no ID') );
    }
    }
    </script>


    --
    Rob

    Comment

    • thomasamillergoogle@yahoo.com

      #3
      Re: find form elements in particular TableRow

      Thanks Rob for working through my assored typos and brain farts. It
      works beautifully now.

      Comment

      Working...