I am trying to add and remove a <select>
drop-down menu in a form by clicking a button. This is the code I have. I could swear that I had this work last night, but when I worked on my project this morning, the drop-down menus will not add / remove correctly.
function DropDowns(){ this.counter = 0; this.addDropdown = function (divname) { var newDiv = document.createElement('div'); var html = '<select name="cookie' + this.counter + '">', i; for (i = 0; i < cookies_drop.length; i++) { html += "<option value='" + cookies_drop[i] + "'>" + cookies_drop[i] + "</option>" } html += '</select>'; newDiv.innerHTML = html; document.getElementById(divname).appendChild(newDiv); this.counter++; } this.remDropdown = function() { $('#dropdowns-container').find('div:last').remove(); this.counter--; } } var dropsTest = new DropDowns();
HTML:
<form action='' method=post id="dropdowns-container"> <button id="add_cookie" type="button" onclick="dropsTest.addDropdown('dropdowns-container');">add cookie</button> <button id="rem_cookie" type="button" onclick="dropsTest.remDropdown();">remove cookie</button> <input name="cookies" type=submit value="submit"> </form>
source share