Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

126
Views
i am trying to make a clear button. when user click on the clear button then all list should be clear permanently

i am trying to make a clear button. when user click on the clear button then all list should be clear permanently. list will appear when the user will input something in input field and save that by SAVE IT button and then list will appear below the save button.

let mySearch = []
    let inputEl = document.getElementById("input-el")
    let saveEl = document.getElementById("save-el")
    let ulEl = document.getElementById("ul-el")
    let clearEl = document.getElementById("clear-el")

    saveEl.addEventListener("click", function () {
        mySearch.push(inputEl.value)
        inputEl.value = " "
        render()
    })

    function render() {
        let listitems = ""
        for (let i =0; i < mySearch.length; i++) {
            listitems += "<li>" + mySearch[i] + "</li>"
            
           // alternative
          /*const list = document.createElement("li")
           list.textContent += mySearch[i]
           ulEl.append(list) */
    }
     ulEl.innerHTML = listitems
    }

    when this clear button click all list which saved by "SAVE IT" button from input field should clear the list
    clearEl.addEventListener("click", function() {
         ulEl.textcontent = " "
    })
<div class="save">
                 <input type="text" id="input-el" class="input" placeholder="Search.."> <br>
                 <button id="save-el" class="savebut">SAVE IT</button>
                 <button type="reset"  id="clear-el" class="clearbut">clear list</button>
                 <ul id="ul-el"></ul>
             </div>

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

this should solve your problem. first you need to remove all the children of the ul element.

    while(ulEl.firstChild){
        ulEl.removeChild(ulEl.firstChild);
    }

and you need to empty the array.

mySearch = []

Full code:

    let mySearch = []
    let inputEl = document.getElementById("input-el")
    let saveEl = document.getElementById("save-el")
    let ulEl = document.getElementById("ul-el")
    let clearEl = document.getElementById("clear-el")
    let save = document.getElementsByClassName("save")[0]

    saveEl.addEventListener("click", function () {
        mySearch.push(inputEl.value)
        inputEl.value = " "
        render()
    })

    function render() {
        let listitems = ""
        for (let i =0; i < mySearch.length; i++) {
            listitems += "<li>" + mySearch[i] + "</li>"
    }
     ulEl.innerHTML = listitems
    }

    clearEl.addEventListener("click", function() {
    while(ulEl.firstChild){
        ulEl.removeChild(ulEl.firstChild);
    }
        mySearch = []
    })
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!