Link to home
Start Free TrialLog in
Avatar of 1desman1
1desman1

asked on

Javascript error

Hi Experts

I have an error that i'm stuck with; here is the code

<script language="JavaScript">
var DomYes=document.getElementById?1:0;

function set_child_listbox(parentObject,childObject,childArray,spanToHide) {

        //Clear child listbox
        for(var i=childObject.length;i>0;i--) {
                childObject.options[i] = null;
        }
       
        childObject.options[0] = new Option("Select Value","");
        var sel_index = parentObject.options[parentObject.selectedIndex].value;
        if (sel_index == "") {
                childObject.disabled = true;
        } else {
                childObject.disabled = false;
                var childIndex = 1;
                for (i = 0; i < childArray.length; i++) {
                        if (childArray[i][1] == sel_index) {
                                childObject.options[childIndex] = new Option(childArray[i][2], childArray[i][0]);
                                childIndex++;
                        }
                }
        }
        //Select first option
        childObject.selectedIndex = 0;

        //Hide dependent grid
        if (spanToHide != "") {
                if (DomYes) {
                        document.getElementById(spanToHide).style.display="none";
                } else {
                        document.all[spanToHide].style.display="none";
                }
        }
}

function reload_page() {
  var sel_index = document.store_productsSearch.s_category_id.options[document.store_productsSearch.s_category_id.selectedIndex].value;
  var sel_subindex = document.store_productsSearch.s_product_id.options[document.store_productsSearch.s_product_id.selectedIndex].value;
  if (sel_subindex != "") {
         document.location.href = document.location.pathname + "?" + "s_product_id=" + sel_subindex+"&s_category_id=" + sel_index;
  }
}

function disable_child_listbox(spanToHide) {
 
  //Disable second listbox
  if (document.store_productsSearch.s_category_id.selectedIndex == "") {
     document.store_productsSearch.s_product_id.disabled = true;
  }    

  //Hide dependent grid
  if (document.store_productsSearch.s_product_id.selectedIndex == "") {
        if (spanToHide != "") {
                if (DomYes) {
                        document.getElementById(spanToHide).style.display="none";
                } else {
                        document.all[spanToHide].style.display="none";
                }
        }
  }    
}

window.onload = function() {
        disable_child_listbox("Products");
}



var Product = new Array(
 
new Array(12,7,"Canvas prints"),
new Array(11,7,"Foam mounted pictures"),
new Array(10,7,"Lasting posters"),
new Array(13,7,"Photographic resizing")
);

</script>

The code is related to 2 list boxes; the first has an option (the 2nd is disabled by default AND isn't 1st error) when the first is selected the 2nd is enabled and the relivant options are populated via the above array

<select onchange="set_child_listbox(this, document.store_productsSearch.s_product_id,Product,'Products');" name="s_category_id">
                        <option value="" selected>Select Value</option>
                        <OPTION VALUE="5">Flyers</OPTION>
<OPTION VALUE="1">Photos</OPTION>
<OPTION VALUE="7">PRECIOUS MEMORIES</OPTION>
<OPTION VALUE="6">Signage Display products</OPTION>
<OPTION VALUE="2">Subscriptions</OPTION>

                      </select>
 </td>
                  </tr>
 
                  <tr class="Controls">
                    <th>Sub category</th>
 
                    <td>
                      <select name="s_product_id">
                        <option value="" selected>Select Value</option>
                       
                      </select>

so there is 2 errors:
1st error is:- The 2nd listbox should be disabled untill an option is shown
2nd error is this line >>>>document.getElementById(spanToHide).style.display="none"; (says object required)

Thanks for reading
Avatar of b0lsc0tt
b0lsc0tt
Flag of United States of America image

1desman1,

1.  Add disabled to the select tag.  Something like this ...

<select name="s_product_id" disabled>

2.  The function getElementById() needs to have the id treated as a string.  For example ...

document.getElementById("spanToHide")

Let me know if you have any questions or need more information.

b0lsc0tt
Avatar of 1desman1
1desman1

ASKER

@ b0lscott

Still the error with

2.  The function getElementById() needs to have the id treated as a string.  For example ...

document.getElementById("spanToHide")
I looked at the code a little too quick.  spanToHide is a variable isn't it, not the value of the id?  If it is a variable then remove the quote marks.  What does the element look like that has the id of Products?  Please provide that block of code too.  Search the document and make sure you find id="Products" only once.

bol
Why can't you call the function onload of body instead of window.onload
What HTML element has:  id='Products' ?
If you still like to use window.onload, then move your window.onload at the end of your html file:
Example:
<html>
     <head></head>
<body>
--
--
</body>
</html>

<script>
window.onload = function() {
        disable_child_listbox("Products");
}
</script>
@ goops1

That now disables the 2nd list box but still the "Object Required" error on this line >>> document.getElementById(spanToHide).style.display="none";

You mentioned earlier to place this in the body; not knowing javascript very well, how do I do that

Thanks
ASKER CERTIFIED SOLUTION
Avatar of gops1
gops1
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial