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
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');	
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.