Cell click reveals row number

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

    Cell click reveals row number

    I have a table with a dynamic number of rows and cells. From the
    nature of the script I am writing, I cannot use IDs.

    Each row has from 1 to 3 cells. Javascript tables have cells[] and
    rows[].

    cells [] is an array of all the cells of the table, supposedly 1D.
    rows [] is an array of all the rows of the table. You can grab the
    cells from the row click.

    I wish to go the other way

    Is there any way to find out what row was clicked on by the mouse when
    the left most cell is clicked but the script ignores the rest of the
    cells in the row.

  • Matt Kruse

    #2
    Re: Cell click reveals row number

    Camet wrote:
    Is there any way to find out what row was clicked on by the mouse
    when the left most cell is clicked but the script ignores the rest of
    the cells in the row.
    You can just go up the parentNode chain from whatever was clicked until you
    find a TR.
    Then, look at the TR's rowIndex property.

    --
    Matt Kruse




    Comment

    • Camet

      #3
      Re: Cell click reveals row number

      You can just go up the parentNode chain from whatever was clicked until you
      find a TR.
      Then, look at the TR's rowIndex property.
      >
      --
      Matt Kruse

      http://www.AjaxToolbox.com
      I do not know what you mean by parentNode in relation to tables. I
      have only used those in relation to reading xml files.

      Comment

      • Evertjan.

        #4
        Re: Cell click reveals row number

        Camet wrote on 06 jul 2006 in comp.lang.javas cript:
        >You can just go up the parentNode chain from whatever was clicked
        >until you find a TR.
        >Then, look at the TR's rowIndex property.
        >>
        >--
        >Matt Kruse
        >http://www.JavascriptToolbox.com
        >http://www.AjaxToolbox.com
        >
        I do not know what you mean by parentNode in relation to tables. I
        have only used those in relation to reading xml files.
        >
        >
        In DOM like in any other tree structure,
        all elements exept the root["global"]
        have a parent[node].

        DOM elements are nearly people, who have two.

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

        Comment

        • Camet

          #5
          Re: Cell click reveals row number

          In DOM like in any other tree structure,
          all elements exept the root["global"]
          have a parent[node].
          >
          DOM elements are nearly people, who have two.
          >
          --
          Evertjan.
          The Netherlands.
          (Please change the x'es to dots in my emailaddress)
          Okay, can you please give an example of this in commented code?

          Thanks

          Camet

          Comment

          • Lasse Reichstein Nielsen

            #6
            Re: Cell click reveals row number

            "Camet" <silver_animal@ hotmail.comwrit es:
            I do not know what you mean by parentNode in relation to tables. I
            have only used those in relation to reading xml files.
            The XML files are probably read into a W3C DOM structure. The same
            thing happens with HTML in a browser, and there is a ECMAScript
            binding for the DOM objects and methods that specifies how to use
            it:
            <URL:http://www.w3.org/TR/DOM-Level-2-HTML/ecma-script-binding.html>

            In this case, you can do something like:

            function findRowNumber(e lement) { // element is a descendent of a tr element
            while(element.t agName.toLowerC ase() != "tr") {
            element = element.parentN ode; // breaks if no "tr" in path to root
            }
            return element.rowInde x;
            }

            and call it when clicking on the table:

            <table
            onclick="doSome thingWith(findR owNumber(event. target||event.s rcElement));">
            ...
            </table>

            /L
            --
            Lasse Reichstein Nielsen - lrn@hotpop.com
            DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
            'Faith without judgement merely degrades the spirit divine.'

            Comment

            • Evertjan.

              #7
              Re: Cell click reveals row number

              Lasse Reichstein Nielsen wrote on 06 jul 2006 in comp.lang.javas cript:
              "Camet" <silver_animal@ hotmail.comwrit es:
              >
              >I do not know what you mean by parentNode in relation to tables. I
              >have only used those in relation to reading xml files.
              >
              The XML files are probably read into a W3C DOM structure. The same
              thing happens with HTML in a browser, and there is a ECMAScript
              binding for the DOM objects and methods that specifies how to use
              it:
              <URL:http://www.w3.org/TR/DOM-Level-2-HTML/ecma-script-binding.html>
              >
              In this case, you can do something like:
              >
              function findRowNumber(e lement) { // element is a descendent of a tr
              element
              while(element.t agName.toLowerC ase() != "tr") {
              element = element.parentN ode; // breaks if no "tr" in path to
              root
              }
              return element.rowInde x;
              }
              >
              and call it when clicking on the table:
              >
              <table
              onclick="doSome thingWith(findR owNumber(event. target||event.s rcElemen
              t));"...
              </table>
              In IE HTML I simply do:

              <table border=1>
              <tr><td onclick='whichR owandCell(this) '>aaaaa
              <td onclick='whichR owandCell(this) '>bbbbb
              <tr><td onclick='whichR owandCell(this) '>cccc
              <td onclick='whichR owandCell(this) '>ddd
              </table>

              <script type='text/javascript'>
              function whichRowandCell (x){
              alert('rowIndex : '+x.parentNode. rowIndex+
              '\ncellIndex: '+x.cellIndex)
              }
              </script>

              Should one expect a element between the tr and the td?
              I think not.




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

              Comment

              • Lasse Reichstein Nielsen

                #8
                Re: Cell click reveals row number

                "Evertjan." <exjxw.hannivoo rt@interxnl.net writes:
                Should one expect a element between the tr and the td?
                I think not.
                Not in valid HTML, no. If you put the onclick on the table and use
                event.target (with IE fallback to event.srcElemen t), then you might
                need to traverse upwards to find the cell and row (but on the other
                hand, you should also remember, as I didn't, the case where you click
                on the table between rows.

                /L
                --
                Lasse Reichstein Nielsen - lrn@hotpop.com
                DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
                'Faith without judgement merely degrades the spirit divine.'

                Comment

                Working...