How to remove dom element? [duplicate] - javascript

This question already has answers here:
JavaScript DOM remove element
(4 answers)
Closed 8 years ago.
Ok I've been so spoiled with Jquery that I don't even know how to remove an element anymore with plain Javascript.
So I have this which I would like to remove with Javascript.
Any help appreciated.

Try this
function RemoveElement(elemID) {
var elem = document.getElementById(elemID);
if (elem.parentNode) {
elem.parentNode.removeChild(elem);
}
}

Node.removeChild is the method, see documentation here: https://developer.mozilla.org/En/DOM/Node.removeChild
Example:
var parent = document.getElementById("mydiv");
var child = document.getElementById("other");
parent.removeChild(child);

like:
container.removeChild(child);

Related

EventListener will not work on clones [duplicate]

This question already has answers here:
Javascript infamous Loop issue? [duplicate]
(5 answers)
Closed 7 years ago.
http://jsfiddle.net/1wqdmo4o/
var whichSelected = document.querySelectorAll(".selected");
for(var i = 0; i < whichSelected.length; i++) {
var clone = whichSelected[i].cloneNode(false);
clone.addEventListener("click", function() {createOutline(clone)});
document.body.appendChild(clone);
}
I have no idea why the event listener will not work on the clones. Any ideas is appreciated!
You are setting the z-index of the copied node to -1, so when clicking, you click on body.
Also, the id is the same as the copied node, you might want to change this.

How can I get the function of a event (debuging jquery) [duplicate]

This question already has answers here:
Can I find events bound on an element with jQuery?
(9 answers)
Closed 9 years ago.
for example I have:
asdasd
...
//js code in other place or other file hard to find, I have this:
$('#activador').click(simplefunction); or
$(document).on('click','#activador',simplefunction); or
$('#activador').live('click',simplefunction);
function simplefunction(){
alert('hello');
}
...
How can I find if the html ('#activador') have a function attached, and how can I find what function the element ('#activador') will trigger?
I know using
var clickEvents = $('#activador').data("events").click;
but I cannot use something like this:
var clickEvents = $('#activador').data("events").on; or
var clickEvents = $('#activador').data("events").live;
You can use _data function to find out event associated with that element,
console.log($._data($('#activador'), "events").click);

How to tell if an HTML element has a particular class in JavaScript? [duplicate]

This question already has answers here:
How can I check in JavaScript if a DOM element contains a class?
(8 answers)
Check if an element contains a class in JavaScript?
(30 answers)
Closed 9 years ago.
Is there an easy way to tell if an HTML element has a specific class? For example:
var element = document.getElementById('something');
if (element.class == 'car')
Of course an element can have multiple classes, so maybe the if statement would have to be of the following form?
if (element.class.includes('car'))
var element = document.getElementById("myid");
if (element.classList.contains("myclass")) { /* do stuff */ }
Read more on element#classList here: https://developer.mozilla.org/en-US/docs/DOM/element.classList
This link also contains a polyfill for older browsers.
If using jQuery is an option you can use hasClass() see http://api.jquery.com/hasClass/
If not you can take a look at pure JS implementation - Test if an element contains a class?
For strange reasons the name of the member containing the class(es) is className, not class. Multiple class names are separated by space.
You need to use:
class = document.getElementById("{id_of_element").getAttribute("class");
then
String[] vals = class.split(" ");
var match = false;
for (i = 0; i < vals.length;i++) {
if (vals[i].equalsIgnoreCase('car') {
match = true;
break;
}
}
if (match) {
//do something
}
HTH.

after() inserting element, then getting it back [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why does jQuery .after() not chain the new element?
This reference code:
$("#id").after(string);
Does a pretty good job inserting the element of the string where its need to.
How can I get a reference to newly inserted HTML element (string)?
var string = '<div id="some_HTML"><span>hello kitty</span></div>';
$jq_elem = $(string); //if it's not a jQuery object, make it one
$("#id").after($jq_elem); //insert into DOM
$jq_elem.css('color', 'red'); //still available
​
FIDDLE
Try using insertAfter:
var $str = $(string).insertAfter('#id');
This will work if string is HTML.

empty jquery element [duplicate]

This question already has answers here:
Getting an empty JQuery object
(4 answers)
Closed 8 years ago.
Is there an elegant way to create an empty jquery element (versus null) ?
$myEmptyElement = $("#ThisIdDoesNotExist234343");
The rational is not checking later on for null.
Later we do :
$myEmptyElement.destroy();
Sure:
var $emptyElement = $();
Why would you want one, though?
A simple example would be:
var $emptyElement = $();
One way:
var $emptyElement = new $;

Categories

Resources