Results 1 to 6 of 6

Thread: When is this table populated?

  1. #1
    Join Date
    Aug 2012

    When is this table populated?

    Here's a webpage that's giving me trouble (the code is largely borrowed, including the "hasClass" function):

    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">

    <script type="text/javascript" language="JavaScript">
    // this function is needed to work around
    // a bug in IE related to element attributes
    function hasClass(obj) {
    var result = false;
    if (obj.getAttributeNode("class") != null) {
    result = obj.getAttributeNode("class").value;
    return result;

    function traversetable(id) {

    // obtain a reference to the desired table
    // if no such table exists, abort
    var table = document.getElementById(id);
    if (! table) { return; }

    // by definition, tables can have more than one tbody
    // element, so we'll have to get the list of child
    // &lt;tbody&gt;s
    var tbodies = table.getElementsByTagName("tbody");

    // and iterate through them...
    for (var h = 0; h < tbodies.length; h++) {

    // find all the &lt;tr&gt; elements...
    var trs = tbodies[h].getElementsByTagName("tr");

    // ... and iterate through them
    for (var i = 0; i < trs.length; i++) {

    // get all the cells in this row...
    var tds = trs[i].getElementsByTagName("td");

    // and iterate through them...
    for (var j = 0; j < tds.length; j++) {

    var mytd = tds[j];




    <table id="mytable" border="1" width="100%">

    <script type="text/javascript">
    traversetable ("mytable");



    In this page, I'm trying to write a JavaScript function function traversetable(id) which is intended to take the table "id" and run through all the tds. As presented above, it just issues a "document.write" for each td.

    I've set up a table with one row and three columns and called this function on it. But the table seems to be empty when "traversetable" runs on it. My "document.write(mytd)" just returns "[object]" for each of the three times it is called. These are output immediately after the table is shown in the normal way with the three proper values in it.

    How can I arrange things so that "traversetable" will find the values I am putting in the table? Or can anyone explain the order in which things happen in a webpage with JavaScript like this one?

    Thanks for any insights.

  2. #2
    Join Date
    Oct 2010
    Versailles, France
    At first, do not use document.write in a function. This instruction works only at the opening of the page when the document is open.

    Then tds[j] is effectively an object which contains some (j=0), words (j=1) and here(j=2). Replace your document.write with an alert(tds[j].innerHTML) to see the content of the cells or build a string with something like this
    var str;
    for (var j = 0; j < tds.length; j++) str+='|'+tds[j].innerHTML;
    // To display the result in an existing paragraph with an id="rsp"
    //HTML after the table
    <p id="rsp"></p>

  3. #3
    Join Date
    Aug 2012
    Thank you, that was helpful and enabled me to move on to my next problem!

    Here's the new problem webpage:


    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">

    <script type="text/javascript" language="JavaScript">

    var optsel=0

    // this function is needed to work around
    // a bug in IE related to element attributes
    function hasClass(obj) {
    var result = false;
    if (obj.getAttributeNode("class") != null) {
    result = obj.getAttributeNode("class").value;
    return result;

    function ShowText (optsel) {
    alert ("Starting in ShowText, optsel " + optsel );
    if (optsel == 1) {
    document.getElementById("mydiv").style.display = "none";
    alert ("Hiding text")
    else {
    document.getElementById("mydiv").style.display = "inline";
    alert ("Showing text")
    alert ("Finishing ShowText");
    return true

    function ActOnForm (myform) {
    for (k=0; k<myform.Rogha.length;k++){
    if (myform.Rogha[k].checked){
    window.optsel = k;
    alert ("In function, optsel " + window.optsel)
    ShowText (window.optsel)
    return true


    <form onsubmit="return ActOnForm(this)">
    <tr><td><input type="radio" value="" name="Rogha" checked />Show</td></tr>
    <tr><td><input type="radio" value="" name="Rogha" />Hide</td></tr>
    <tr><td align="center"><input type="Submit" value="Submit" /></td></tr>

    <div id="mydiv">Some text to show or hide.</div>

    <script type="text/javascript">
    alert ("optsel is " + optsel)
    \\ ShowText (optsel)



    The page just contains a <div>. I use a <form> to read some user input to control how the <div> is displayed. The <form> has two radio buttons labelled "Show" and "Hide". "Show" is the default.

    Clicking the "Submit" button of the <form> executes the function ActOnForm, to put 0 or 1 in a global variable "optsel", depending to which radio button is selected. "optsel" is initialised to 0.

    "optsel" is used by a script function ShowText to set the "style.display" of the <div>. I have two candidate places to call ShowText: one is where you see it in the source above, at the end of ActOnForm; the other is in an unnamed script at the very end of the page, where it is commented out in the above.

    We can use the alerts to trace what happens.

    When the page is displayed at first, the alert in the unnamed script shows that "optsel" is 0, and the div is shown.

    Now click the "Hide" radio button and submit. ActOnForm is executed, with two alerts: the first just confirms "Selecting", and the second that "optsel" has the value 1.

    The next thing we see from the alerts is that ShowText has started (with optsel = 1), that the <div> is being hidden (and indeed it is gone), and that ShowText is finishing. Looking good! But not for long. As soon as I clear that last alert, the radio button reverts to "Show" and the <div> reappears.

    This is with the call to ShowText positioned in ActOnForm, as in the source above. If I reposition the call to ShowText to the end of the page, the only difference is that ShowText has optsel = 0, and the <div> is therefore shown throughout.

    What I want to do is make permanent the situation which I had brought about before the page refreshed itself. It there a way to do that?

    Thanks again for any help.

  4. #4
    Join Date
    Aug 2012
    I've found the following solution by experimentation. I have no idea why this happens, perhaps someone can explain.

    Modify my previous page as follows.

    Remove <form onsubmit="return ActOnForm(this)">

    Remove </form>

    Replace <input type="Submit" value="Submit" />

    by <input type="Button" onclick="ActOnForm()" value="Submit" />

    In ActOnForm,
    Replace (myform) in the function header by ()
    Replace myform. by window. twice elsewhere

    It seems that the <form onsubmit= > has something to do with making the page revert, whereas <input type="Button" onclick= > avoids this.

  5. #5
    Join Date
    Aug 2012
    Hmm my solution works in IE7, but not it seems in FireFox or Google Chrome, where clicking an input button not in a form seems to do nothing. So I'm still looking for a browser-independent solution.

  6. #6
    Join Date
    Aug 2012
    Back to my message of yesterday, the last one that had a full webpage source on it.

    In function ActOnForm, change "return true" to "return false".

    According to what I see on the web, that is enough to prevent the page refreshing. So far, it seems to work on all browsers.

Thread Information

Users Browsing this Thread

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

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.12288 seconds
  • Memory Usage 2,895KB
  • Queries Executed 13 (?)
More Information
Template Usage (33):
  • (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
  • (1)bbcode_code
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (6)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (6)postbit
  • (6)postbit_onlinestatus
  • (6)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 (70):
  • 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_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_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