Urgent! Javascripts Onchage event question.

Posted on 2003-12-07
Last Modified: 2008-02-01
Hello experts,

I not expert in Javascripts, but I want to do this in Javascript and still not success.
I get this free Javascript from internet and modified some to meet my requirements which all the data in the select boxes are retrived from database. Then come the taugh part which not provided in the script. I want the script to automatically check the available number (also from database) after the second selection (select 2).

For example: I select first select box and it automatically do onchange script to effect select box 2. Then I select second select box and I want it to automatically checked the checkboxes(disabled) that in used. The rest checkbox is remain uncheck.

Let Say:

Select 1 :
subselect 1
subselect 2

Select 2:
subsubselect 1
subsubselect 2
subsubselect 3

I select subselect1 -> subsubselect2 -> (available number are 1,4,14,28). These 4 checkboxes should automatically checked with disabled and the rest remain uncheck.


below is my code:

<META name=description content=Cascading Menu generated at SuperTom.Com>

<!-- original code and docs at -->
<!-- you need this stuff below -->
<META content=history name=save>
<STYLE>.saveHistory {
      BEHAVIOR: url(#default#savehistory)
<!-- you need this stuff above-->

<SCRIPT language=JavaScript>

<SCRIPT language=JavaScript1.1>
if (typeof(Option)+"" != "undefined") v=true;

<SCRIPT language=JavaScript>
// Menugen Copyright 2000 Tom Melendez
// Universal Related Select Menus - cascading popdown menus
// by Andrew King. v1.34 19990720
// Copyright (c) 1999 LLC. All Rights Reserved.
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
// Originally published and documented at
// Contact for all other uses.
// Change History
// 3-3-96 code optimized, modularized, and made universal-abk
// 3-8-97 jumpPage1 and 2 consolidated into generalized func
// 3-10-97 changeMenu added jumpPage(0,0), for non-ns3 browsers
// make popup act like a live popup when no related functionality
// 8-5-97 browser detect script changed to check for option functionality
// 9-11-97 aln changed to options.length, as IE 4 does not support
// nulling out options
// 12-22-98 added SIZE att to make select lists, used named forms/elements
// 1-1-99 added getFormNum to fix netscape 2 bug (doesn't support named fms)
//  pass formName, elementNum to relate and jmp as workaround
//  pass this.form,0 and increment instead of decrement in relate to get
//   next form
//  assumes menu2 follows menu1 in HTML
// tested in mac ie3.01/ns2.02 (live), ns 3.01,4.05,4.5 and ie4.01 related
// 990204 - adopted a more modular/oo approach using two D arrays, thanks
//   to Jacob Berner <> for his input
// 990701 - fixed back button bug in ie4+ by resetting all forms onload-abk
//    thanks to peter belesis ( for this fix
// 990714 - added IE5 persistence - abk pb
// 990715 - went open source, created stripped down example files - abk
// 990720 - jmp() fix getFormNum(formName);->
//     var formNum = getFormNum(formName);
//  if (v) {..} added around newCat array creation for old browsers
// 990721 - getFormNum/relate tweaks, unnec code removed,
//     ns2.0 mac fix - getFormNum returns -1 in nested tables
//     new jmp bypasses prob - thx to thanks to Michael Guitton
// 990722 - var for scope added, jmp chgd to named form for ns2.02 bug-abk
if(v){a=new Array(22);}

function getFormNum (formName) {
      var formNum =-1;
      for (i=0;i<document.forms.length;i++){
            tempForm = document.forms[i];
            if (formName == tempForm) {
                  formNum = i;
      return formNum;

var catsIndex = -1;
var itemsIndex;

if (v) { // ns 2 fix
function newCat(){
      a[catsIndex] = new Array();
      itemsIndex = 0;

function O(txt,url) {
      a[catsIndex][itemsIndex]=new myOptions(txt,url);

function myOptions(text,value){
      this.text = text;
      this.value = value;

// fill array here

mysql_connect("$dbhost", "$dbuser", "$dbpass") OR die("Couldn't connect to MySQL");
mysql_select_db("$dbname") OR die("Couldn't open db. Error if any was: ".mysql_error() );

$q = "SELECT DISTINCT fld1 from tbl1";
$r = mysql_query($q);

while ($rows=mysql_fetch_array($r)) {
      echo "newCat();";
      $q1="SELECT DISTINCT fld2 from tbl1 where fld1='$t'";
      $r1 = mysql_query($q1);
      while ($rows1=mysql_fetch_array($r1)) {
            $w = $rows1["fld2"];
            echo 'O("'.$w.'","'.$w.'");';

} // if (v)

function relate(formName,elementNum,j) {
        var formNum = getFormNum(formName);
         if (formNum>=0) {
              formNum++; // reference next form, assume it follows in HTML
              with (document.forms[formNum].elements[elementNum]) {
                    for(i=options.length-1;i>0;i--) options[i] = null; // null out in reverse order (bug workarnd)
                          options[i] = new Option(a[j][i].text,a[j][i].value);
                    options[0].selected = true;
    else {

// BACK BUTTON FIX for ie4+- or
// from peter belesis:
// IE4+ remembers the index of each SELECT but NOT the CONTENTS of each
// SELECT, so it gets it wrong.
// it has to do with MEMORY CACHE (where form input is stored) and how
// IE stores information about SELECT menus.
// IE stores the selectedINDEX ONLY of the SELECT menu, not the
// when we return to a page, it displays the default contents of each
// SELECT, grabs the stored index from cache and aligns the default
// contents to that index.
// Netscape, on the other hand, seems to remember both INDEX and CONTENTS
// added ie5 persistence 990714

function IEsetup(){
      if(!document.all) return;
      IE5 = navigator.appVersion.indexOf("5.")!=-1;
      if(!IE5) {
            for (i=0;i<document.forms.length;i++) {

window.onload = IEsetup;


<BODY bgColor=#ffffff>

<TABLE bgColor=#FFFFFF border=1 cellPadding=0 cellSpacing=0 bordercolor=#000000>

<TABLE bgColor=#FFFFFF border=0 cellPadding=4 cellSpacing=2>
    <TD vAlign=top>select 1</td>
      <TD vAlign=top>
      <FORM action= method=post name=f1 onsubmit="return false;">
      <SELECT class=saveHistory id=m1 name=m1 onchange=relate(this.form,0,this.selectedIndex)>
      $sql = "SELECT DISTINCT fld1 from tbl1";
      $rsql = mysql_query($sql);

      while ($rw = mysql_fetch_array($rsql)) {
            $w = $rw["fld1"];
            <option value="<?=$rw["fld1"]?>"><?echo $rw["fld1"];?></option>
      <? } ?>
    <TD vAlign=top>select 2</td>
      <TD vAlign=top>
      <FORM action= method=post name=f2 onsubmit="return false;">
      <SELECT class=saveHistory id=m2 name=m2>
      $sql = "SELECT DISTINCT fld2 from tbl1";
      $rsql = mysql_query($sql);

      while ($rw = mysql_fetch_array($rsql)) {
            $w = $rw["fld2"];
            <option value="<?=$rw["fld2"]?>"><?echo $rw["fld2"];?></option>
      <? } ?>

  <table width="446" border="0">
        <input type="checkbox" name="checkbox" value="checkbox">
        1 </td>
        <input type="checkbox" name="checkbox2" value="checkbox">
        2 </td>
        <input type="checkbox" name="checkbox3" value="checkbox">
        3 </td>
        <input type="checkbox" name="checkbox4" value="checkbox">
        4 </td>
        <input type="checkbox" name="checkbox5" value="checkbox">
        5 </td>
        <input type="checkbox" name="checkbox6" value="checkbox">
        6 </td>
        <input type="checkbox" name="checkbox7" value="checkbox">
        7 </td>
        <input type="checkbox" name="checkbox8" value="checkbox">
        8 </td>
        <input type="checkbox" name="checkbox9" value="checkbox">
        9 </td>
        <input type="checkbox" name="checkbox10" value="checkbox">
        10 </td>
        <input type="checkbox" name="checkbox11" value="checkbox">
        11 </td>
        <input type="checkbox" name="checkbox12" value="checkbox">
        12 </td>
        <input type="checkbox" name="checkbox13" value="checkbox">
        13 </td>
        <input type="checkbox" name="checkbox14" value="checkbox">
        14 </td>
        <input type="checkbox" name="checkbox15" value="checkbox">
        15 </td>
        <input type="checkbox" name="checkbox16" value="checkbox">
        16 </td>
        <input type="checkbox" name="checkbox17" value="checkbox">
        17 </td>
        <input type="checkbox" name="checkbox18" value="checkbox">
        18 </td>
        <input type="checkbox" name="checkbox19" value="checkbox">
        19 </td>
        <input type="checkbox" name="checkbox20" value="checkbox">
        20 </td>
        <input type="checkbox" name="checkbox21" value="checkbox">
        21 </td>
        <input type="checkbox" name="checkbox22" value="checkbox">
        22 </td>
        <input type="checkbox" name="checkbox23" value="checkbox">
        23 </td>
        <input type="checkbox" name="checkbox24" value="checkbox">
        24 </td>
        <input type="checkbox" name="checkbox25" value="checkbox">
        25 </td>
        <input type="checkbox" name="checkbox26" value="checkbox">
        26 </td>
        <input type="checkbox" name="checkbox27" value="checkbox">
        27 </td>
        <input type="checkbox" name="checkbox28" value="checkbox">
        <input type="checkbox" name="checkbox29" value="checkbox">
        29 </td>
        <input type="checkbox" name="checkbox30" value="checkbox">
        30 </td>

Experts out there, please help me.

Question by:zamirjalil
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
LVL 16

Expert Comment

ID: 9894422
Something like this

<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
function getSelectValue(selObj){
      return selObj.options[selObj.selectedIndex].value;
function checkDisabledBoxes(fObj){
      // 1 4 14 28
      if(getSelectValue(fObj.sel2) == "subsubselect2"){
            fObj.checkbox1.checked = true;
            //fObj.checkbox1.disabled = true;
            fObj.checkbox4.checked = true;
            //fObj.checkbox4.disabled = true;
            fObj.checkbox14.checked = true;
            //fObj.checkbox14.disabled = true;
            fObj.checkbox28.checked = true;
            //fObj.checkbox28.disabled = true;
<form name="form1" method="post" action="">
<select name="sel2" onchange="checkDisabledBoxes(this.form);">
<option value="subsubselect2">subsubselect2</option>
<input type="checkbox" name="checkbox1" disabled value="y">
<input type="checkbox" name="checkbox4" disabled value="y">
<input type="checkbox" name="checkbox14" disabled value="y">
<input type="checkbox" name="checkbox28" disabled value="y">

LVL 16

Expert Comment

ID: 9894425
You do know if its checked and disabled, you will not get the checked value in the final results?

Author Comment

ID: 9894507
thanks jay, actually the final result will be checked and disabled + enable checkboxes. What javascript will do is to validate any availabe number (checkboxes) from the sql query. Disabled means not available. But it is still display all the checkboxes.

What you did is correct but I want the checkbox is enable wht the page is load. Only after the second selection, then certain checkbox will disabled. this will be checked thru sql query.

LVL 16

Accepted Solution

jaysolomon earned 250 total points
ID: 9896476
so change this

<input type="checkbox" name="checkbox1" disabled value="y">
<input type="checkbox" name="checkbox4" disabled value="y">
<input type="checkbox" name="checkbox14" disabled value="y">
<input type="checkbox" name="checkbox28" disabled value="y">

to this

<input type="checkbox" name="checkbox1" value="y">
<input type="checkbox" name="checkbox4" value="y">
<input type="checkbox" name="checkbox14" value="y">
<input type="checkbox" name="checkbox28" value="y">

and un comment the script

like so

function checkDisabledBoxes(fObj){
     // 1 4 14 28
     if(getSelectValue(fObj.sel2) == "subsubselect2"){
          fObj.checkbox1.checked = true;
          fObj.checkbox1.disabled = true;
          fObj.checkbox4.checked = true;
          fObj.checkbox4.disabled = true;
          fObj.checkbox14.checked = true;
          fObj.checkbox14.disabled = true;
          fObj.checkbox28.checked = true;
          fObj.checkbox28.disabled = true;

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Understanding UserAgent String 2 49
Can't get js code to execute 8 33
Question regarding to $.ajax() with type "POST" vs $.post() 4 33
arrays and buttons with user input 2 34
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (, we'll extend the program by adding a depth-…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question