Feature
Post

Category
Code

Hide Table Rows with onChange()

On occasion, a developer will require that certain elements of a page stay invisible to users while others become visible, depending on a user's action. In this case, the user makes a selection on a drop-down menu and the selected table row becomes visible while the rest remain hidden.

First, we establish the script and begin to define the function:

JAVASCRIPT:
  1. <!--
  2.  
  3. function changeRow() {

Next, we find the number of elements in the select menu. This count is especially useful for dynamically-generated menus when the developer may not always know the number of available elements at design time:

JAVASCRIPT:
  1. var intCount = document.getElementById("selTest").options.length; //gets count of items in select menu

We also need the number in the zero-indexed array of the selected element:

JAVASCRIPT:
  1. var intValue = document.getElementById("selTest").selectedIndex; //gets number (0 index) of selected item

Now we loop through the number of elements in the menu:

JAVASCRIPT:
  1. for (i=0;i

With the document.getElementById(strValue) function, a developer can assign the area he'd prefer to make visible. We're using table rows here, but this same technique applies to any div, span, or form field.

The onchange() function in the select menu calls the function that sets the selected row's style:

HTML:
  1. <form method="post" action="#">
  2.     <table id="tbTable" cellpadding="2" cellspacing="2" border="0">
  3.         <tr>
  4.             <td>Change Here
  5.             </td>
  6.             <td>
  7.                
  8.                     Row 1
  9.                     Row 2
  10.                     Row 3
  11.                
  12.             </td>
  13.             <td>
  14.             </td>
  15.         </tr>

For each row, we assign the id value to the range of options available in the menu. Each row has its default style set to visibility:hidden;. Again, this is very useful for database-generated forms and tables:

HTML:
  1. <tr style="hidden;">
  2.             <td>Row 1a</td>
  3.             <td>Row 1b</td>
  4.             <td>Row 1c</td>
  5.         </tr>
  6.         <tr style="hidden;">
  7.             <td>Row 2a</td>
  8.             <td>Row 2b</td>
  9.             <td>Row 2c</td>
  10.         </tr>
  11.         <tr style="hidden;">
  12.             <td>Row 3a</td>
  13.             <td>Row 3b</td>
  14.             <td>Row 3c</td>
  15.         </tr>
  16.     </table>
  17. </form>

If you need the elements in a page to cover the same area, you can change the visibility:hidden; lines to display:none; and the visibility:visible; to display:;. This script is also useful for any desired style change (font size, font color, etc.). For such a simple script, it packs a wide variety of uses.

Demo: View a simple HTML file that does this!

  1. By Nick posted on August 20, 2008 at 5:04 pm
    Want an avatar? Get a gravatar! • You can link to this comment

    For accessibility purposes I suggest looping through TR’s with javascript and add the style.visibility = “hidden” so that in the event a user has JavaScript disabled they will still see the table values. See my example below


    function hideRows() {
    var allRows = document.getElementsByTagName("tr");
    for (i=0; i < allRows.length; i++) {
    if(allRows[i].id != “option”) {
    allRows[i].style.visibility=’hidden’;
    }
    }
    }
    function changeRow() {
    var intCount = document.getElementById(”selTest”).options.length; //gets count of items in select menu
    var intValue = document.getElementById(”selTest”).selectedIndex; //gets number (0 index) of selected item
    for (i=0;i<intCount;i++){
    var strValue = document.getElementById(”selTest”)[i].value; //gets value of selected item
    if (i == intValue) {
    document.getElementById(strValue).style.visibility=’visible’;
    }
    else {
    document.getElementById(strValue).style.visibility=’hidden’;
    }
    }
    }
    // Event Handler
    function addEvent(obj, evType, fn) {
    if (obj.addEventListener){
    obj.addEventListener(evType, fn, false);
    return true;
    }
    else if (obj.attachEvent) {
    var r = obj.attachEvent(”on”+evType, fn);
    return r;
    }
    else { return false; }
    }
    addEvent(window, ‘load’, hideRows);

    P.S. Your code snippets are not showing everything. I had to go to demo page to see all the code.

  2. By Jose Velez posted on September 27, 2008 at 9:17 pm
    Want an avatar? Get a gravatar! • You can link to this comment

    very good tutorial

  3. Trackback

    Your words are your own, so be nice and helpful if you can. If this is the first time you're posting a comment, it might go into moderation. Don't worry, it's not lost, so there's no need to repost it! We accept clean XHTML in comments, but don't overdo it please.