www.webdeveloper.com
Results 1 to 15 of 15

Thread: Get value of a loop generated table cell via onClick

  1. #1
    Join Date
    Mar 2011
    Posts
    5

    Question Get value of a loop generated table cell via onClick

    Hello,

    I am trying to get the value of a table cell that is generated by a php loop. The purpose is so eventually I can click a row out of a list and have it take me to a form view of the list item that is clicked. I've searched and searched for the last few days for something with no luck. I run across a lot of hits that tell me I need to have an id for everysingle thing I click on but there has to be a better way.

    function as best I could make



    Code:
    function test() {
    var elTableRow = document.getElementById("tr");
    var elTableCells = elTableRow.getElementsByTagName("gtrpart");
    alert(elTableCells[0].innerText);
    }

    and my loop

    PHP Code:
    echo "<table class='info' border='2'align='center'width='1050px'>";

    $color1 "#CCCCCC";
    $color2 "#FFFFFF";
    $row_count 0;

    echo 
    "<tr class='tablehead'> <th style='display: none;'>GTR Part Number</th> <th>Part Number</th> <th>REV</th> <th>Description</th> <th>Customer</th> <th>Qty</th> <th>Location</th> </tr>";

    while(
    $row mysqli_fetch_array$result )) 

    {
        
    $row_color = ($row_count 2) ? $color1 $color2;
        
        echo 
    "<tr onClick='test()' class='data' bgcolor='$row_color'><td style='display: none;' align='center'>";  
        echo 
    $row['gtr_part_number'];
        echo 
    "</td><td align='center'>";
        echo 
    $row['part_number'];    
        echo 
    "</td><td align='center'>"
        echo 
    $row['rev'];
        echo 
    "</td><td align='center'>"
        echo 
    $row['description'];
        echo 
    "</td><td align='center'>"
        echo 
    $row['customer'];
        echo 
    "</td><td align='center'>"
        echo 
    $row['qty'];
        echo 
    "</td><td align='center'>"
        echo 
    $row['location'];
        echo 
    "</td></tr>";
        
    $row_count++; 
    As you can see in my function I right now only want an alert box to pop up telling me the value of the cell I clicked on. Is there a way to do this?

    Any help would be greatly appreciated

    Thanks,
    Austin

  2. #2
    Join Date
    Dec 2002
    Location
    St. Louis, MO, USA
    Posts
    1,582
    Every element should have a unique id, yes. That is how you tell JavaScript what "container" to give you the value, or contents, of.

    ^_^

  3. #3
    Join Date
    Mar 2011
    Posts
    5
    So then how would I call a specific row when I click it? lets say I set the whole table row to a unique value that it gets from the loop, how would I call that specific row from my function without having 1000+ functions?

  4. #4
    Join Date
    Dec 2002
    Location
    St. Louis, MO, USA
    Posts
    1,582
    <tr>
    <td id="gtr_num_1" onclick="functionName(this.id);"></td>
    <td id="part_num_1" onclick="functionName(this.id);"></td>
    <td id="rev_1" onclick="functionName(this.id);"></td>
    <td id="description_1" onclick="functionName(this.id);"></td>
    ...
    </tr>
    <tr>
    <td id="gtr_num_2" onclick="functionName(this.id);"></td>
    <td id="part_num_2" onclick="functionName(this.id);"></td>
    <td id="rev_2" onclick="functionName(this.id);"></td>
    <td id="description_2" onclick="functionName(this.id);"></td>
    ...
    </tr>
    Code:
    function functionName(tid) { // tid is the variable that contains the ID of the cell calling the function..
      var thisContent = document.getElementById(tid).firstChild;
    alert(thisContent);
     }
    ^_^

    PS.. if you want CELL text, you don't put the ID on the TR, you put it on the TD.
    Last edited by WolfShade; 03-08-2011 at 12:34 PM.

  5. #5
    Join Date
    Mar 2011
    Posts
    5
    Ok I tried doing that I copy pasted the function you wrote and modified my loop to this..

    PHP Code:
    while($row mysqli_fetch_array$result )) 

    {
        
    $row_color = ($row_count 2) ? $color1 $color2;
        
    $gtrpart $row['gtr_part_number'];
        
    $part $row['part_number'];
        echo 
    "<tr class='data' bgcolor='$row_color'><td id='" $gtrpart "'onClick='functionName(this.id)'; style='display: none;' align='center'>"
        echo 
    $row['gtr_part_number'];
        echo 
    "</td><td id='" $part "' onClick='functionName(this.id)'; align='center'>";
        echo 
    $row['part_number'];
    .... 
    the alert works fine but instead of getting a value like I'm expecting I'm getting

    "[object text]"

    so what am I doing wrong?

    and thank you you're keeping me from pulling my hair out

  6. #6
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Quote Originally Posted by WolfShade View Post
    var thisContent = document.getElementById(tid).firstChild;
    No. A textNode is an Element/Object as well. Should be:
    Code:
    var thisContent=document.getElementById(tid).firstChild.nodeValue;
    //or
    var thisContent=document.getElementById(tid).firstChild.data;
    //or
    var thisContent=document.getElementById(tid).innerText||document.getElementById(tid).textContent; // IE||full DOM compliant browsers
    But if the container element has no textNode - is empty - not even a blank space within (like <td></td>) all those will return "undefined".
    Last edited by Kor; 03-08-2011 at 01:54 PM.

  7. #7
    Join Date
    Dec 2002
    Location
    St. Louis, MO, USA
    Posts
    1,582
    ^ What he said ^

  8. #8
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Quote Originally Posted by WolfShade View Post
    ^ What he said ^
    ? What he said what? I saw your code, and I have noticed that it is not correct. A textNode is an object, thus if you want to return its "stringed content" you must do it properly.

  9. #9
    Join Date
    Dec 2002
    Location
    St. Louis, MO, USA
    Posts
    1,582
    Learn to speak American, Kor.. I am agreeing with you. The arrows were pointed to YOUR post.

    ^_^
    Last edited by WolfShade; 03-08-2011 at 02:07 PM.

  10. #10
    Join Date
    Mar 2011
    Posts
    5
    That worked beautifully

    Thank you both so much!

    I think I might owe you guys a six pack

  11. #11
    Join Date
    Dec 2002
    Location
    St. Louis, MO, USA
    Posts
    1,582
    Glad you got it working, don't worry about the sixer - I'm in STL(MO), Kor is in Romania. But thanks for the thought.

    ^_^

  12. #12
    Join Date
    Mar 2011
    Posts
    5
    Either way thanks again

    I'm sure I'll be back though with another question!

  13. #13
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Quote Originally Posted by WolfShade View Post
    Learn to speak American, Kor.. I am agreeing with you. The arrows were pointed to YOUR post.

    ^_^
    Since when the arrows are "American" ?

    Just kidding. No offense Anyway, I am proud about my British English, so... you know...

  14. #14
    Join Date
    Dec 2002
    Location
    St. Louis, MO, USA
    Posts
    1,582
    Funny, I didn't see a British accent.

  15. #15
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Quote Originally Posted by WolfShade View Post
    Funny, I didn't see a British accent.
    The British accent is never to be seen. It is to be heard

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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

"

"

X vBulletin 4.2.2 Debug Information

  • Page Generation 0.16439 seconds
  • Memory Usage 3,034KB
  • Queries Executed 15 (?)
More Information
Template Usage (35):
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (3)bbcode_code
  • (2)bbcode_php
  • (4)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (15)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (15)postbit
  • (15)postbit_onlinestatus
  • (15)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (73):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates