dimanche 22 novembre 2020

How can I create a conditional statement to dynamically create a button for each ID without using only IFs? Should it be a nested IF statement? [closed]

    let button = document.createElement("Button");
    let buttonTxt = document.createTextNode("Select Times");
    button.appendChild(buttonTxt);

This is a form where people have to input their names and the button I'm creating is for team members to select their available times.

    function createButton(){
      
        let name1 = document.getElementById("name1"); 
        let name2 = document.getElementById("name2");
        let name3 = document.getElementById("name3");
        let name4 = document.getElementById("name4");
        let name5 = document.getElementById("name5");

        if (name1 && name1.value) { // if there is a name in the input, a button will be created for their section
            document.getElementById('b1').appendChild(button);
        } 
        if (name2 && name2.value) {
            document.getElementById('b2').appendChild(button);
        } 
        if (name3 && name3.value) {
            document.getElementById('b3').appendChild(button);
        } 
        if (name4 && name4.value) {
            document.getElementById('b4').appendChild(button);
        } 
        if (name5 && name5.value) {
            document.getElementById('b5').appendChild(button);
        } 
       

If statements work individually but when put in a function it doesn't work anymore.

Aucun commentaire:

Enregistrer un commentaire