Home > Mobile >  Disabling a button on empty <select> list?
Disabling a button on empty <select> list?

Time:01-09

`I've created a form that allows a user to enter their details and have their first and last names displayed along with a numerical ID. There is a 'Delete' button allowing the user to remove whatever name they select.

by default, the 'Delete' button should be disabled until the <select> list is populated. If all names are deleted from the list - the 'Delete' button should disable again.

That's where im struggling. I cant get the 'Delete' button to enable when a name is added to the list.

Below is my HTML and JavaScript:

    MemberList
    


    <h1>Member List</h1>

    <form method="post" id="_frmFull">

        <label for="lstMembers">Member:</label>
        <select size="10"
                name="lstMembers"
                id="lstMembers">
        </select>


        <button type="button"
                id="addMember"
                name="Add Member">
                Add Member
        </button>
        <button type="button"
                name="Delete Member"
                id="delete">
                Delete Member</button>

            <div id="Modal" >

                <div >

                    <label for="memTitle">Title:</label>
                    <input list="memTitles" name="memTitles" id="memTitle">

                    <datalist id="memTitles">
                        <option value="Dr">
                        <option value="Mr">
                        <option value="Mrs">
                        <option value="Ms">
                    </datalist>

                    <label for="firstName">First Name:</label>
                    <input type="text" name="firstName" id="firstName" required="required">

                    <label for="middleName">Middle Name (optional):</label>
                    <input type="text" name="middleName" id="middleName">

                    <label for="lastName">Last Name:</label>
                    <input type="text" name="lastName" id="lastName" required="required">

                    <br><br>

                    <label for="Country">Country:</label>
                    <input list="countries" name="Country" id="Country">
                    <datalist id="countries">
                        <option value="Australia">
                        <option value="United Kingdom">
                        <option value="America">
                        <option value="New Zealand">
                    </datalist>

                    <label for="Gender">Gender:</label>
                    <select name="Gender" id="Gender">
                        <option>Male</option>
                        <option>Female</option>
                        <option>Other</option>
                    </select>

                    <label for="birthDate">Birthdate:</label>
                    <input type="date"
                           id="birthDate"
                           min="1900-01-01"
                           max="2022-12-31" required="required">

                    <br><br>

                    <div id="resiAddress">

                    <p>Residential Address:</p>

                    <br>

                    <label for="txtResidentialAddress">Address:</label>
                    <input type="text" required="required" id="txtResidentialAddress" oninput="copyAddressFields();">


                    <br><br>

                    <label for="txtResiPostCode">Postcode:</label>
                    <input type="text" required="required" id="txtResiPostCode">

                    <br><br>

                    <label for="txtResiSuburb">Suburb:</label>
                    <input type="text" required="required" id="txtResiSuburb">

                    <br><br>

                    <label for="txtResiCountry" >Country</label>
                    <input type="text" required="required" id="txtResiCountry">

                    <br><br>

                    <label for="txtResiState">State:</label>
                    <input type="text" required="required" id="txtResiState">

                </div>

                    <br><br>

                    <label for="chkSynchronizeAddresses">Same as residential </label>
                    <input type="checkbox" required="required" id="chkSynchronizeAddresses" >

                    <div id="postAddress">

                        <p>Postal Address:</p>

                        <br>

                        <label for="txtPostalAddress">Address:</label>
                        <input type="text" id="txtPostalAddress">

                        <br><br>

                        <label for="txtPostCode">Postcode:</label>
                        <input type="text" id="txtPostCode">

                        <br><br>

                        <label for="txtPostalSuburb">Suburb:</label>
                        <input type="text" id="txtPostalSuburb">

                        <br><br>

                        <label for="txtPostalCountry">Country:</label>
                        <input type="text" id="txtPostalCountry">

                        <br><br>

                        <label for="txtPostalState">State:</label>
                        <input type="text" id="txtPostalState">

                    </div>

                    <br><br>

                    <label for="txtPhone" >Phone: (optional)</label>
                    <input id="txtPhone" type="text">


                    <br><br>

                    <label for="txtMobile" >Mobile: (optional)</label>
                    <input id="txtMobile" type="text">


                    <br><br>

                    <button type="button" name="save" id="save">Save</button>

                    <br><br>

                    <button type="button"
                            name="cancel"
                            id="closeModal">
                            Cancel</button>
                </div>

            </div>

        </form>

<script>

    console.log("Log test - please work!!")

    const addBtn = document.getElementById("addMember");
    const modal = document.getElementById("Modal");
    const cancelBtn = document.getElementById("closeModal");
    let chkSynchronizeAddresses = document.getElementById("chkSynchronizeAddresses");
    const _frmFull = document.getElementById("_frmFull");
    let Member = document.getElementById("lstMembers");
    let Delete = document.getElementById("delete");
    let save = document.getElementById("save");
    let fName = document.getElementById("firstName");
    let lName = document.getElementById("lastName");
    let birthDate = document.getElementById("birthDate");

    Delete.disabled = Member.value === 1;



    addBtn.onclick = function (){
        openPopUp();
        console.log('function - correct');
    }

    cancelBtn.onclick = function (){
        let closeTrue = confirm("Close form? You've not saved anything.")
        if (closeTrue === true) {
            closePopUp();
        }
    }

    chkSynchronizeAddresses.onclick = function (){
        console.log("Is this working??");

        if (chkSynchronizeAddresses.checked){
            document.getElementById("txtPostalAddress").setAttribute("readonly", "true");
            document.getElementById("txtPostCode").setAttribute("readonly", "true");
            document.getElementById("txtPostalState").setAttribute("readonly", "true");
            document.getElementById("txtPostalSuburb").setAttribute("readonly", "true");
            document.getElementById("txtPostalCountry").setAttribute("readonly", "true");
            copyAddressFields();
        }
        else {
            document.getElementById("txtPostalAddress").removeAttribute("readonly");
            document.getElementById("txtPostCode").removeAttribute("readonly")
            document.getElementById("txtPostalState").removeAttribute("readonly");
            document.getElementById("txtPostalSuburb").removeAttribute("readonly");
            document.getElementById("txtPostalCountry").removeAttribute("readonly");
        }


    }

    function copyAddressFields () {
        if (chkSynchronizeAddresses.checked){
            document.getElementById("txtPostalAddress").value = document.getElementById("txtResidentialAddress").value;
            document.getElementById("txtPostCode").value = document.getElementById("txtResiPostCode").value;
            document.getElementById("txtPostalSuburb").value = document.getElementById("txtResiSuburb").value;
            document.getElementById("txtPostalState").value = document.getElementById("txtResiState").value;
            document.getElementById("txtPostalCountry").value = document.getElementById("txtResiCountry").value;

        }

    }

    let idCount = 0

    save.onclick = function (){

        let fNameValue = fName.value
        let lNameValue = lName.value
        let bDateValue = birthDate.value
        let pCountryValue = document.getElementById("txtPostalCountry").value
        let pSuburbValue = document.getElementById("txtPostalSuburb").value
        let pStateValue = document.getElementById("txtPostalState").value
        let pCodeValue = document.getElementById("txtPostCode").value
        let pAddressValue = document.getElementById("txtPostalAddress").value

        if (fNameValue === ""){
            alert("Please enter your first name to proceed");
            return false;
        }

        else
        if (lNameValue === "") {
            alert("Please enter your last name to proceed");
            return false;
        } else if (bDateValue === "") {
            alert("Please select a DOB to proceed");
            return false;
        } else if (pCountryValue === "") {
            alert("Please enter your country to continue");
            return false;
        } else if (pSuburbValue === "") {
            alert("Please enter your suburb to continue");
            return false;
        } else if (pCodeValue === "") {
            alert("Please enter your postcode to continue");
            return false;
        } else if (pAddressValue === "") {
            alert("Please enter your Address to continue");
            return false;
        } else if (pStateValue === "") {
            alert("Please enter your state to continue");
            return false;
        } else {

            Member.innerHTML  =
             `<option value= >${idCount} ${_frmFull.firstName.value} ${_frmFull.lastName.value}<option>`
            idCount  
            console.log("check - is increase even working????")

        }

        closePopUp();
        return true;

    }
    function closePopUp() {
        modal.style.display="none";
    }
    function openPopUp() {
        modal.style.display = "block";
    }
    
    Delete.onclick = function removeOption () {
        Member.remove(Member.options);
        console.log("is remove being triggered??")
    }

    if (Member.options.length == ""){
        Delete.disabled = true
    }
    else {
        Delete.disabled = false
    }

</script>
</body>

`

CodePudding user response:

The reason why the Delete button is not enabling is because the code that is supposed to enable the Delete button is only run once, when the page loads.

Assuming that this is the code that is supposed to enable the button:

if (Member.options.length == 0){ // changed from Member.options.length == ""
    Delete.disabled = true
} else {
    Delete.disabled = false
}

This code is only run once, when the page loads and the JavaScript code is run. In order to enable the button properly, we need to run this code every time that Member.options.length changes. One way you can do this is place the code snippet above for disabling/enabling the Delete button inside the onclick listener for the save button and to re-check every time a new user is saved. You would have to add this snippet into every place in your code where Member.options.length could change (inside the listener for the delete button as well).

Full updated code:

        MemberList
    


    <h1>Member List</h1>

    <form method="post" id="_frmFull">

        <label for="lstMembers">Member:</label>
        <select size="10"
                name="lstMembers"
                id="lstMembers">
        </select>


        <button type="button"
                id="addMember"
                name="Add Member">
                Add Member
        </button>
        <button type="button"
                name="Delete Member"
                id="delete">
                Delete Member</button>

            <div id="Modal" >

                <div >

                    <label for="memTitle">Title:</label>
                    <input list="memTitles" name="memTitles" id="memTitle">

                    <datalist id="memTitles">
                        <option value="Dr">
                        <option value="Mr">
                        <option value="Mrs">
                        <option value="Ms">
                    </datalist>

                    <label for="firstName">First Name:</label>
                    <input type="text" name="firstName" id="firstName" required="required">

                    <label for="middleName">Middle Name (optional):</label>
                    <input type="text" name="middleName" id="middleName">

                    <label for="lastName">Last Name:</label>
                    <input type="text" name="lastName" id="lastName" required="required">

                    <br><br>

                    <label for="Country">Country:</label>
                    <input list="countries" name="Country" id="Country">
                    <datalist id="countries">
                        <option value="Australia">
                        <option value="United Kingdom">
                        <option value="America">
                        <option value="New Zealand">
                    </datalist>

                    <label for="Gender">Gender:</label>
                    <select name="Gender" id="Gender">
                        <option>Male</option>
                        <option>Female</option>
                        <option>Other</option>
                    </select>

                    <label for="birthDate">Birthdate:</label>
                    <input type="date"
                           id="birthDate"
                           min="1900-01-01"
                           max="2022-12-31" required="required">

                    <br><br>

                    <div id="resiAddress">

                    <p>Residential Address:</p>

                    <br>

                    <label for="txtResidentialAddress">Address:</label>
                    <input type="text" required="required" id="txtResidentialAddress" oninput="copyAddressFields();">


                    <br><br>

                    <label for="txtResiPostCode">Postcode:</label>
                    <input type="text" required="required" id="txtResiPostCode">

                    <br><br>

                    <label for="txtResiSuburb">Suburb:</label>
                    <input type="text" required="required" id="txtResiSuburb">

                    <br><br>

                    <label for="txtResiCountry" >Country</label>
                    <input type="text" required="required" id="txtResiCountry">

                    <br><br>

                    <label for="txtResiState">State:</label>
                    <input type="text" required="required" id="txtResiState">

                </div>

                    <br><br>

                    <label for="chkSynchronizeAddresses">Same as residential </label>
                    <input type="checkbox" required="required" id="chkSynchronizeAddresses" >

                    <div id="postAddress">

                        <p>Postal Address:</p>

                        <br>

                        <label for="txtPostalAddress">Address:</label>
                        <input type="text" id="txtPostalAddress">

                        <br><br>

                        <label for="txtPostCode">Postcode:</label>
                        <input type="text" id="txtPostCode">

                        <br><br>

                        <label for="txtPostalSuburb">Suburb:</label>
                        <input type="text" id="txtPostalSuburb">

                        <br><br>

                        <label for="txtPostalCountry">Country:</label>
                        <input type="text" id="txtPostalCountry">

                        <br><br>

                        <label for="txtPostalState">State:</label>
                        <input type="text" id="txtPostalState">

                    </div>

                    <br><br>

                    <label for="txtPhone" >Phone: (optional)</label>
                    <input id="txtPhone" type="text">


                    <br><br>

                    <label for="txtMobile" >Mobile: (optional)</label>
                    <input id="txtMobile" type="text">


                    <br><br>

                    <button type="button" name="save" id="save">Save</button>

                    <br><br>

                    <button type="button"
                            name="cancel"
                            id="closeModal">
                            Cancel</button>
                </div>

            </div>

        </form>

<script>

    console.log("Log test - please work!!")

    const addBtn = document.getElementById("addMember");
    const modal = document.getElementById("Modal");
    const cancelBtn = document.getElementById("closeModal");
    let chkSynchronizeAddresses = document.getElementById("chkSynchronizeAddresses");
    const _frmFull = document.getElementById("_frmFull");
    let Member = document.getElementById("lstMembers");
    let Delete = document.getElementById("delete");
    let save = document.getElementById("save");
    let fName = document.getElementById("firstName");
    let lName = document.getElementById("lastName");
    let birthDate = document.getElementById("birthDate");
  
    Delete.disabled = Member.options.length == 0


    addBtn.onclick = function (){
        openPopUp();
        console.log('function - correct');
    }

    cancelBtn.onclick = function (){
        let closeTrue = confirm("Close form? You've not saved anything.")
        if (closeTrue === true) {
            closePopUp();
        }
    }

    chkSynchronizeAddresses.onclick = function (){
        console.log("Is this working??");

        if (chkSynchronizeAddresses.checked){
            document.getElementById("txtPostalAddress").setAttribute("readonly", "true");
            document.getElementById("txtPostCode").setAttribute("readonly", "true");
            document.getElementById("txtPostalState").setAttribute("readonly", "true");
            document.getElementById("txtPostalSuburb").setAttribute("readonly", "true");
            document.getElementById("txtPostalCountry").setAttribute("readonly", "true");
            copyAddressFields();
        }
        else {
            document.getElementById("txtPostalAddress").removeAttribute("readonly");
            document.getElementById("txtPostCode").removeAttribute("readonly")
            document.getElementById("txtPostalState").removeAttribute("readonly");
            document.getElementById("txtPostalSuburb").removeAttribute("readonly");
            document.getElementById("txtPostalCountry").removeAttribute("readonly");
        }


    }

    function copyAddressFields () {
        if (chkSynchronizeAddresses.checked){
            document.getElementById("txtPostalAddress").value = document.getElementById("txtResidentialAddress").value;
            document.getElementById("txtPostCode").value = document.getElementById("txtResiPostCode").value;
            document.getElementById("txtPostalSuburb").value = document.getElementById("txtResiSuburb").value;
            document.getElementById("txtPostalState").value = document.getElementById("txtResiState").value;
            document.getElementById("txtPostalCountry").value = document.getElementById("txtResiCountry").value;

        }

    }

    let idCount = 0

    save.onclick = function (){

        let fNameValue = fName.value
        let lNameValue = lName.value
        let bDateValue = birthDate.value
        let pCountryValue = document.getElementById("txtPostalCountry").value
        let pSuburbValue = document.getElementById("txtPostalSuburb").value
        let pStateValue = document.getElementById("txtPostalState").value
        let pCodeValue = document.getElementById("txtPostCode").value
        let pAddressValue = document.getElementById("txtPostalAddress").value

        if (fNameValue === ""){
            alert("Please enter your first name to proceed");
            return false;
        }

        else
        if (lNameValue === "") {
            alert("Please enter your last name to proceed");
            return false;
        } else if (bDateValue === "") {
            alert("Please select a DOB to proceed");
            return false;
        } else if (pCountryValue === "") {
            alert("Please enter your country to continue");
            return false;
        } else if (pSuburbValue === "") {
            alert("Please enter your suburb to continue");
            return false;
        } else if (pCodeValue === "") {
            alert("Please enter your postcode to continue");
            return false;
        } else if (pAddressValue === "") {
            alert("Please enter your Address to continue");
            return false;
        } else if (pStateValue === "") {
            alert("Please enter your state to continue");
            return false;
        } else {

            Member.innerHTML  =
             `<option value= >${idCount} ${_frmFull.firstName.value} ${_frmFull.lastName.value}<option>`
            idCount  
            console.log("check - is increase even working????")
            if (Member.options.length == 0){
                Delete.disabled = true
            }
            else {
                Delete.disabled = false
            }

        }

        closePopUp();
        return true;

    }
    function closePopUp() {
        modal.style.display="none";
    }
    function openPopUp() {
        modal.style.display = "block";
    }
    
    Delete.onclick = function removeOption () {
        Member.remove(Member.options);
        if (Member.options.length == 0) {
          Delete.disabled = true
        }
        else {
          Delete.disabled = false
        }
        console.log("is remove being triggered??")
    }

</script>
</body>

CodePudding user response:

so if i understood correctly you want to toggle a button if a is populated

In that case you want to keep track of the options

You can use

document.querySelectoAll('options') 

and store it in a variable

then write a function that check if there's a value in that variable and disable or enable the button based on that.

Here's a fiddle:

https://jsfiddle.net/skm7bcr6/15/

  • Related