JavaScript: clear all elements from unordered list

15 09 2012

For HTML unordered list like this one:

<ul id="mylist">
	<li>my list item</li>
</ul>

JavaScript can help to remove all of its elements. Put this function in page <head>:

<script type="text/javascript">
	function clearlist (){ 
		var elem = document.getElementById('mylist');
		elem.parentNode.removeChild(elem);
	} 
</script>

and call it, for example on button onclick event:

onclick="clearlist()"

sometimes it may be better to call it inline, without placing script in page <head>, like this:

onclick="document.getElementById(mylist').parentNode.removeChild(document.getElementById('mylist'));"

Did I help you?
I manage this blog and share my knowledge for free sacrificing my time. If you appreciate it and find this information helpful, please consider making a donation in order to keep this page alive and improve quality

Donate Button with Credit Cards

Thank You!


Actions

Information

Give Your feedback:

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




%d bloggers like this: