Jump to content

[SOLVED] Combo enable/disable


lucy

Recommended Posts

Why does the following code work in dreamweaver live view but not firefox or internet explorer?

 

Its meant to enable the second combo box if the first value is 1 and enable the third combo box if the first value is 2. code below:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>

<script type="text/javascript">
function updateList1(optionValue){

if(optionValue=='1'){list2.disabled = false;}else {list2.disabled = true;} 
if(optionValue=='2'){list3.disabled = false;}else {list3.disabled = true;}
}

</script>
</head>

<body>

    <select name="list1" id="list1" onchange="updateList1(this.value)">
    <option selected></option>
    <option value="1">1</option>
    <option value="2">2</option>
    </select>
    <select name="list2" id="list2" disabled="disabled">
    <option selected></option>
    <option value="one">one</option>
    <option value="two">two</option>
    </select>
    <select name="list3" id="list3" disabled="disabled">
    <option selected></option>
    <option value="apple">apple</option>
    <option value="pie">pie</option>
    </select>
    </select>

</body>
</html>

 

Thanks for any help :)

Lucy

Link to comment
https://forums.phpfreaks.com/topic/173676-solved-combo-enabledisable/
Share on other sites

It works for me in IE7 but not in FF3.

 

I believe the problem is that your reference to the other two select lists is not complete. It looks liek the code is trying to reference them only by their name, but not giving any "context" of that name. If they were in a form you could reference them via the form object like so document.forms['formName'].elements['fieldname']

 

But, in this case I would just use getElementById(). Also, you don't need full If/Esle satements, simply assign the disabled value of the other two select lists based upon the value of select list 1.

 

function updateList1(optionValue)
{
    document.getElementById('list2').disabled = (optionValue!='1');	
    document.getElementById('list3').disabled = (optionValue!='2');	
}

Um:

function updateList1(optionValue)
{
    document.getElementById('list2').disabled = (optionValue!='1');
    document.getElementById('list3').disabled = (optionValue!='2');
    if (optionValue=='3')
    {
        alert('foo');
    }
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.