How to clear previous appended data on change the dropdown menu in JavaScript ?
To clear the previously appended data on change the drop-down is carried out by using jQuery. Appended data must be cleared on change or on toggling the drop-down menu, otherwise previously appended data append with current menu item data. To avoid this issue, jQuery or JavaScript is implemented with help of add(), remove(), addClass(), and removeClass() methods.
The Following Approaches will explain clearly:
Approach 1: Initially use removeClass() method to remove active class of previously appended data menu item. Then followed by addClass() method to add active class of currently appended data menu item. Now use each() function to append data with respect active class added or removed on click.
- Example: Below Alert choosing example illustrate to clear the previously appended data on change the drop-down using remove(), add() methods.
<!DOCTYPE html><htmllang="en"><head><metacharset="utf-8"><metaname="viewport"content="width=device-width, initial-scale=1"><linkrel="stylesheet"href="<scriptsrc="</script><scriptsrc="</script><scriptsrc="</script></head><body><divclass="container"><h1class="pt-3 text-success font-weight-bold text-center">GeeksforGeeks</h1><divclass="m-3 dropdown"><buttontype="button"class="btn btn-primary dropdown-toggle"data-toggle="dropdown">Alert Menu</button><divclass="dropdown-menu"><aclass="dropdown-item"href="#">Success</a><aclass="dropdown-item"href="#">Warning</a><aclass="dropdown-item"href="#">Danger</a></div></div><pid="alert"class="alert alert-dismissible"><buttontype="button"class="close"data-dismiss="alert">×</button><iid="demo"></i> Alert ! ...</p></div><script>$("a").click(function() {// removeClass active of previously selected menu item$('a.dropdown-item.active').removeClass("active");// Add class active to current selected menu item$(this).addClass("active");// Getting text from within selected elementsvar msg = $('a.dropdown-item.active').text();// If condition to check selected alert messageif (msg == "Success") {$("#alert").removeClass("alert-warning");$("#alert").removeClass("alert-danger");$("#alert").addClass("alert-success");} else if (msg == "Warning") {$("#alert").removeClass("alert-success");$("#alert").removeClass("alert-danger");$("#alert").addClass("alert-warning");} else {$("#alert").removeClass("alert-success");$("#alert").removeClass("alert-warning");$("#alert").addClass("alert-danger");}var str = "";$(".active").each(function() {// Using html() to append html datastr += $(this).html() + " ";});$("#demo").html(str);}).click();</script></body></html> - Output:

Approach 2: Initially use for loop remove active class of previously appended data menu item, here loop condition based on length of item listed with help of remove() method. Then followed by add() method to add active class of currently appended data menu item listed. Now use each() function to append data with respect to active class added or removed while toggling listed items.
- Example: Below active courses selection example illustrate to clear the previously appended data onchange the dropdown using addClass(), removeClass() methods.
<!DOCTYPE html><htmllang="en"><head><metacharset="utf-8"><metaname="viewport"content="width=device-width, initial-scale=1"><linkrel="stylesheet"href="<scriptsrc="</script><scriptsrc="</script><scriptsrc="</script></head><body><divclass="container"><h1class="pt-3 text-success font-weight-bold text-center">GeeksforGeeks</h1><divclass="m-3 dropdown"><buttontype="button"class="btn btn-primary dropdown-toggle"data-toggle="dropdown">Active Courses Menu</button><divclass="dropdown-menu"><aclass="dropdown-item"href="#">JAVA Backend Development</a><aclass="dropdown-item"href="#">DSA Foundation</a><aclass="dropdown-item"href="#">GEEK Class</a><aclass="dropdown-item"href="#">Machine Learning Foundation With Python & AI</a><aclass="dropdown-item"href="#">Competitive Programming</a></div></div><divclass="card"><divclass="card-header bg-primary text-light">Selected Courses</div><divclass="card-body p-3 bg-lighttext-dark borderborder-primary"><h4id="demo"></h4></div><divclass="card-footer bg-primary text-light">Available @ GeeksforGeeks</div></div></div><script>$(".dropdown-item").click(function() {// Select all list itemsvar dropItems = $(".dropdown-item");var str = "";// Remove 'active' tag for all list items// based on iterationfor (let i = 0; i <dropItems.length; i++) {dropItems[i].classList.remove("active");}// Add 'active' tag for currently selected itemthis.classList.add("active");$(".active").each(function() {// Using text() to append text datastr += $(this).text() + " ";});$("#demo").text(str);});</script></body></html> - Output:



