Can an else if statement be used to create a step-wise reveal of elements with JavaScript?

Edit: Thank you! This problem is resolved. This was my first StackExchange question and you’ve all been very kind in responding so quickly, insightfully and thoroughly.

I’m building a page that has an “add another task” button. Each time it’s pressed, one hidden element is supposed to change CSS properties from “hidden” to “visible”, adding one more row of inputs to the page. I’ve managed to get them all to reveal at once, but I’m trying to get the rows to show one at a time.

Here’s what I’ve written so far. Else if statements made the most sense to me, based on the SE posts I read about them, but with the first press of the button, both hidden rows (“task2” and “task3”) reveal simultaneously. Am I using the wrong kind of conditional statement? Or do the statements need to be inside their own functions within the showNextTask function? I’d appreciate any help!

function showNextTask() {
                let task2 = document.querySelector("#task-2-row");
                let task3 = document.querySelector("#task-3-row");

                if ((task2.style.visibility === "hidden")) {
                    task2.style.visibility = "visible";
                    task2.style.maxHeight = "80px";
                } else if (
                    (task2.style.visibility === "visible") &&
                    (task3.style.visibility === "hidden")
                ) {
                    task3.style.visibility = "visible";
                    task3.style.maxHeight = "80px";
                }
            }

let nextInputTask = document.querySelector("#add-another-task-button");
nextInputTask.addEventListener("click", showNextTask);

As requested below, here is the HTML of Task 2 (Task 3 is identical):

<div id="task-2-row">
                    <form id="add-task-2">
                        <span class="add-task-2-description">
                            <input
                                type="text"
                                placeholder="Task 2"
                                autocomplete="off"
                                id="task-input-bar-2"
                                class="task-input-field"
                            />
                        </span>
                        <span class="add-task-2-time">
                            <input
                                type="time"
                                id="timer-field-2"
                                class="task2 timer-field"
                                name="appt"
                            />
                            <input
                                type="submit"
                                id="submit-task-2-button"
                                class="button add-timer-button"
                                value="Add Task"
                            />
                        </span>
                    </form>
                </div>

And the CSS:

    #task-2-row {
                visibility: hidden;
                max-height: 0;
            }

New contributor

J Park is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

9

You are using Assignment operator = instead of equality operator == or ===

Instead I suggest you can try to make a more generic function to add N number of task with the button

Try

 <style>
    .task-row {
        visibility: hidden;
        max-height: 0;
        overflow: hidden;
        transition: max-height 0.3s ease-out, visibility 0.3s ease-out;
        margin-bottom: 10px;
    }
</style>
<body>

<button id="add-another-task-button">Add Task</button>
<div id="task-container">
    <!-- Tasks will be created dynamically -->
</div>

<script>
    let taskCount = 0;

    function addNewTask() {
        taskCount++;
        const taskContainer = document.querySelector("#task-container");
        const newTask = document.createElement("div");
        newTask.id = `task-${taskCount}-row`;
        newTask.className = "task-row";
        newTask.innerHTML = `Task ${taskCount}: <input type="text">`;

        taskContainer.appendChild(newTask);

        // Make new task visible
        setTimeout(() => {
            newTask.style.visibility = "visible";
            newTask.style.maxHeight = "80px"; 
        }, 0);
    }

    document.addEventListener("DOMContentLoaded", (event) => {
        let nextInputTask = document.querySelector("#add-another-task-button");
        nextInputTask.addEventListener("click", addNewTask);
    });
</script>

This approach will help to create any number of task with ‘Add Task’ CTA.

1

Just to pinpoint why your if..else doesn’t work:

You’re checking task2.style.visibility, but that style was never set. Instead you have a CSS class that applies to that element, but that is not the same thing. To know how all applicable CSS styles affect the visibility of task2 you could use getComputedStyle. Then it will work.

Another remark: in the else case you already know that task2 is visible, so there is no need to explicitly check for that again.

So change this:

if ((task2.style.visibility === "hidden")) {
    task2.style.visibility = "visible";
    task2.style.maxHeight = "80px";
} else if (
    (task2.style.visibility === "visible") &&
    (task3.style.visibility === "hidden")
) {
    task3.style.visibility = "visible";
    task3.style.maxHeight = "80px";
}

to this:

if ((getComputedStyle(task2).visibility === "hidden")) {
    task2.style.visibility = "visible";
    task2.style.maxHeight = "80px";
} else if (getComputedStyle(task3).visibility === "hidden") {
    task3.style.visibility = "visible";
    task3.style.maxHeight = "80px";
}

But as noted, this is not really a scalable approach. Also, playing with maxHeight like that really shows you want to use the display property and not visibility. Note that display takes different values, so make sure to look it up.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật