The disabled property can be controlled directly from the parent list. This keeps presentation logic in JavaScript instead of generating JavaScript statements from PHP.
const category = document.querySelector('#category');
const subcategory = document.querySelector('#subcategory');
subcategory.disabled = true;
category.addEventListener('change', () => {
subcategory.disabled = category.value === '';
});
If the second list is loaded from the server, keep it disabled until the new options have arrived successfully.
<SCRIPT language=JavaScript>
<!--
function reload(form)
{
var val=form.cat.options[form.cat.options.selectedIndex].value;
self.location='dd.php?cat=' + val;
}
function disableselect()
{
<?Php
if(isset($cat) and strlen($cat) > 0){
echo "document.f1.subcat.disabled = false;";}
else{echo "document.f1.subcat.disabled = true;";}
?>
}
//-->
</script>
We will collect the value of $cat before this JavaScript code as based on this value our command inside the JavaScript will change.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.
| Dheeraj | 07-04-2014 |
| It Is Not Enabling After Selecting The First Drop Down ... :( plz help | |
| sanket dharurkar | 24-06-2015 |
| I want to create a basic form in php without use of jscript. I want to display the second drop down list only when I select an item from first drop down list like any banking form where after selecting the country it will only display the states in that country. Please, help me ASAP. | |