SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Apr 2005
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Which row of table?

    My table is generated by php. Never know how many row in it. Each roe contain a dropdown which MAY be used by the user. If it is, there is javascript work to be done. Am using "onchange" method of select. In the function to handle it, how can I tell which row of the table the select is on?

    Thanks

  2. #2
    SitePoint Zealot
    Join Date
    Jun 2009
    Location
    Ottawa, Ontario, Canada
    Posts
    120
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In order to answer your question it would be very handy to have the code that is giving you issues.

    In order to assist you it would be really helpful for me to know how that onchange event is triggered and what the function looks like.

    Also, are you able to modify the PHP code? It should not be necessary but it may lead to a simpler solution.

    cheers,
    floater

  3. #3
    Function Curry'er JimmyP's Avatar
    Join Date
    Aug 2007
    Location
    Brighton, UK
    Posts
    2,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code JavaScript:
    // Assuming you're not using any library...
     
    // This is the onChange handler for the <select> elements:
    function onChange() {
        var tdParent = getFirstParent(this, 'td');
        var currentRow = getPrevSiblings(tdParent).length + 1;
        alert(currentRow);
    }
     
    // Required helper functions:
     
    function getFirstParent(node, parentNodeName) {
     
        var p = node.parentNode;
     
        if (p) do {
            if ( p.nodeName.toLowerCase() === parentNodeName ) {
                return p;
            }
        } while ( p = p.parentNode );
     
        return null;
     
    }
     
    function getPrevSiblings(node, type) {
     
        type = type || 1;
     
        var prev = [], i = 0;
     
        while ( node = node.previousSibling ) {
            if ( node.nodeType === type ) {
                prev[i++] = node;
            }
        }
     
        return prev;
     
    }
    James Padolsey
    末末末末末末末末末末末末末末末末末末末
    Awesome JavaScript Zoomer (demo here)
    'Ajaxy' - Ajax integration solution (demo here)


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •