How to remove an element using Javascript

To remove an element using javascript, you can use one of the following snippets.

Sample Javascript

//usage document.getElementById("ElementId").remove();
Element.prototype.remove = function() {
	this.parentElement.removeChild(this);
}

Sample Javascript

//usage document.getElementsByClassName("Elements").remove();
NodeList.prototype.remove = HTMLCollection.prototype.remove = function() {
	for(var index = 0, length = this.length; index < length; index++) {
		if(this[index] && this[index].parentElement) {
			this[index].parentElement.removeChild(this[index]);
		}
	}
}

removeChild() is the only method supported by the DOM to remove a element in Javascript (see -> Mozilla MDN Node.removeChild()).

How to empty an array in Javascript

To empty an array in Javascript you can use the following methods.

Sample Javascript

myArray = [];

This code will set the variable A to a new empty array (take care if you have references to the “old” array).

or

myArray.length = 0

This will clear the existing array by setting its length to 0.

or

myArray.splice(0,myArray.length)

Working, but not recommended for this task. see Array.prototype.splice()

or

while(myArray.length > 0) {
    myArray.pop();
}

Fastest solution, without breaking references.

How to check a checkbox with jQuery

To check a checkbox with jQuery you can use the snippet below.

Sample jQuery 1.6+

$('.Checkbox').prop('checked', true); //change true to false to set checked state to false

Sample jQuery 1.5 and below

$('.Checkbox').attr('checked', true);  //change true to false to set checked state to false

How to get selected value of dropdownlist using JavaScript

To get selected value of dropdownlist using JavaScript you can use the following snippet.

Sample Javascript

var dropDown = document.getElementById("MyDropDownId");
var dropdownValue = e.options[e.selectedIndex].value; //get the value
var dropdownText = e.options[e.selectedIndex].text; //get the text