/    Sign up×
Community /Pin to ProfileBookmark

Remove group of elements

In the following script all works fine except the “Remove all items”.

It adds and removes individual elements fine, but when I call the dropElems() function
it removes up to the last 2. If I click it again, it removes only one.
Finally, pressing one more time does remove all.

My question is why. ?
I’m calling the same function to remove one element multiple times
to remove all the elements, but it seems to stop short of the actual desired action.

[code]
<html>
<body>
<script type=”text/javascript”>

var elemCount = 0;
function pushElem() {
elemCount++;
var m = “New Element “+elemCount;
var n = document.createElement(“li”);
n.appendChild(document.createTextNode(m));
document.getElementById(“myList”).appendChild(n);
}

function popElem() {
if (elemCount > 0) {
elemCount–;
document.getElementById(‘myList’).removeChild(document.getElementById(‘myList’).lastChild);
}
}

function dropElems() {
var sel = document.getElementById(‘myList’).getElementsByTagName(‘li’);
alert(sel.length); // for testing purposes only
for (var i=0; i<sel.length; i++) { popElem(); }
}

window.onload = function() {
for (var i=0; i<5; i++) { pushElem(); } // simulated drop-down creation
}

</script>
<input type=”button” value=”Add list item” onClick=”pushElem();”>
<input type=”button” value=”Remove added items” onClick=”popElem();”>
<input type=”button” value=”Remove all items” onClick=”dropElems();”>
<ul id=”myList”></ul>
</body>
</html>
[/code]

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@Declan1991Oct 29.2011 — The length changes everytime you remove an element, which is why you are left with half the elements each time. Anything along these lines will fix it.<i>
</i>function dropElems() {
var l = document.getElementById('myList').getElementsByTagName('li').length;
while (l--) popElem();
}


Though if you go to the bother of maintaining elemCount, I don't know why you don't use:function dropElems() {
while (elemCount--) popElem();
}
Personally I'd stick to using the actual list all the time and never use elemCount.
Copy linkTweet thisAlerts:
@JMRKERauthorOct 29.2011 — Thank you, but that made it worse ?

I changed:
<i>
</i>function dropElems() {
var l = document.getElementById('myList').getElementsByTagName('li');
alert(l.length); // for testing purposes only
while (l--) { popElem(); }
}


I don't get any errors, but it does nothing. Removes none of the elements at all ?

Now what am I doing wrong? ?
Copy linkTweet thisAlerts:
@JMRKERauthorOct 29.2011 — I think I got it.

Changed again to:
<i>
</i>function dropElems() {
var l = document.getElementById('myList').getElementsByTagName('li');
l = l.length;
// alert(l); // for testing purposes only
while (l &gt; 0) { popElem(); l--; alert(l); }
}


Thanks for the guidance.

?
Copy linkTweet thisAlerts:
@Declan1991Oct 29.2011 — It didn't work the first time, because you forget the .length. The code as I posted it is a better version than what you got to work. As a matter of interest, I changed your code slightly for my own testing purposes if you'd like to take a look at it.function List (id) {
var ul = document.getElementById(id);
if (!ul) ul = document.createElement("ul");
var lis = ul.getElementsByTagName("li");
function push(c) {
var n = document.createElement("li");
if (typeof c === "string") {
n.appendChild(document.createTextNode(c));
}
else {
n.appendChild(c);
}
ul.appendChild(n);
}
function pop() {
if (lis.length) {
return ul.removeChild(ul.lastChild);
}
}
function removeAll() {
while (ul.hasChildNodes()) ul.removeChild(ul.lastChild);
}
function getRootEl() {
return ul;
}
function controls() {
var controls = document.createElement("div");
var item = document.createElement("input");
item.type="text";
controls.appendChild(item);
var add = document.createElement("input");
add.type = "button";
add.value = "Add list item";
add.onclick = function () {
push(add.parentNode.firstChild.value);
};
controls.appendChild(add);
var remove = document.createElement("input");
remove.type = "button";
remove.value = "Remove last list item";
remove.onclick = pop;
controls.appendChild(remove);
var all = document.createElement("input");
all.type = "button";
all.value = "Remove all list items";
all.onclick = removeAll;
controls.appendChild(all);
return controls;
}
return {
getRootEl: getRootEl,
push: push,
pop: pop,
removeAll: removeAll,
controls: controls
};
};
window.onload = function() {
var List1 = List("list1");
document.body.appendChild(List1.controls());
document.body.appendChild(List1.getRootEl());
var List2 = List("list2");
document.body.appendChild(List2.controls());
document.body.appendChild(List2.getRootEl());
for (var i = 0; i &lt; 5; i++) {
List1.push("1."+i);
List2.push("2."+i);
}
}
The major changes are that if you define a list in HTML with the same id, it will just add and remove things from that, you can pass strings or DOM to push, and it only ever relies on the actual list.
Copy linkTweet thisAlerts:
@JMRKERauthorOct 29.2011 — I just saw your last post after working on another revision.

I'll take a look at yours if you would comment on this even more generic revision.

Thanks again ?

<i>
</i>&lt;html&gt;
&lt;body&gt;
&lt;script type="text/javascript"&gt;

function pushElem(IDS) {
var m = "New Element "+cntElems(IDS);
var n = document.createElement("li");
n.appendChild(document.createTextNode(m));
document.getElementById(IDS).appendChild(n);
}

function popElem(IDS) {
if (cntElems(IDS) &gt; 0) {
document.getElementById(IDS).removeChild(document.getElementById(IDS).lastChild);
}
}

function cntElems(IDS) {
return document.getElementById(IDS).getElementsByTagName('li').length;
}

function dropElems(IDS) {
while (cntElems(IDS) &gt; 0) { popElem(IDS); }
}

window.onload = function() {
for (var i=0; i&lt;5; i++) { pushElem('myList'); } // simulated drop-down creation
}

&lt;/script&gt;
&lt;input type="button" value="Add list item" onClick="pushElem('myList');"&gt;
&lt;input type="button" value="Remove added item" onClick="popElem('myList');"&gt;
&lt;input type="button" value="Remove all items" onClick="dropElems('myList');"&gt;
myList&lt;br&gt;
&lt;ul id="myList"&gt;&lt;/ul&gt;

&lt;input type="button" value="Add list item" onClick="pushElem('yourList');"&gt;
&lt;input type="button" value="Remove added item" onClick="popElem('yourList');"&gt;
&lt;input type="button" value="Remove all items" onClick="dropElems('yourList');"&gt;
yourList&lt;br&gt;
&lt;ul id="yourList"&gt;&lt;/ul&gt;

&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@JMRKERauthorOct 29.2011 — Ahh, I hadn't yet considered that the contents of the <LI> would need to be changed as well.

Nice feature.

Taking your idea to my version:
<i>
</i>&lt;html&gt;
&lt;body&gt;
&lt;script type="text/javascript"&gt;

function pushElem(IDS,m) {
// var m = "New Element "+cntElems(IDS); // modify for more generic usage
var n = document.createElement("li");
n.appendChild(document.createTextNode(m));
document.getElementById(IDS).appendChild(n);
}

function popElem(IDS) {
if (cntElems(IDS) &gt; 0) {
document.getElementById(IDS).removeChild(document.getElementById(IDS).lastChild);
}
}

function cntElems(IDS) {
return document.getElementById(IDS).getElementsByTagName('li').length;
}

function dropElems(IDS) {
while (cntElems(IDS) &gt; 0) { popElem(IDS); }
}

window.onload = function() {
var msg = ['Element 1','Element 2','Element 3','Element 4','Element 5'];
for (var i=0; i&lt;msg.length; i++) { pushElem('myList',msg[i]); } // simulated drop-down creation
}

&lt;/script&gt;
&lt;input type="text" value="" id="myListInfo"&gt;
&lt;input type="button" value="Add list item"
onClick="pushElem('myList',document.getElementById('myListInfo').value)"&gt;
&lt;br&gt;
&lt;input type="button" value="Remove added item" onClick="popElem('myList');"&gt;
&lt;input type="button" value="Remove all items" onClick="dropElems('myList');"&gt;
&lt;br&gt;myList&lt;br&gt;
&lt;ul id="myList"&gt;&lt;/ul&gt;

&lt;input type="text" value="" id="yourListInfo"&gt;
&lt;input type="button" value="Add list item"
onClick="pushElem('yourList',document.getElementById('yourListInfo').value)"&gt;
&lt;br&gt;
&lt;input type="button" value="Remove added item" onClick="popElem('yourList');"&gt;
&lt;input type="button" value="Remove all items" onClick="dropElems('yourList');"&gt;
&lt;br&gt;yourList&lt;br&gt;
&lt;ul id="yourList"&gt;&lt;/ul&gt;

&lt;/body&gt;
&lt;/html&gt;
×

Success!

Help @JMRKER spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.29,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...