This question already has answers here:
Create <div> and append <div> dynamically
(9 answers)
Closed 2 years ago.
I used a query selector to get the div i want to append. But the created div doesn't want to append to anything other than the body. Is there a way i can make it specifically append under the div?
Look at this: https://www.w3schools.com/jsref/met_node_appendchild.asp
What you need to do is
parent.appendChild(thatDiv);
Related
This question already has answers here:
How can I find elements by text content with jQuery?
(8 answers)
Closed 4 years ago.
how can I remove an element using jquery by the text in the element.
Link1
I dont want to remove the element by class or ID. I want to do something like this:
$('a').html('Link1').remove()
Can you help me with this?
Please try this
jQuery
$('a:contains("Link1")').remove();
Demo fiddle
For more reference please refer this.
This question already has answers here:
.getElementByClassName not working? [duplicate]
(2 answers)
Closed 7 years ago.
Im trying to make all my div class elements to lose all its content by replacing their current content with nothing "".
document.getElementsByClassName("sprint_column").innerHTML = "";
But nothing happens with the "sprint_column" when im trying to reach the class, However if i try to reach the IDs it works:
document.getElementsById("div3_Score").innerHTML = "";
Here is the code where the Ids and Classes is created:
<div class='sprint_column' id='div3_".$team."'>Sprint 1</div>
Is there any way to clear all content from a class with "getElementsByClassName" or do i have to loop through all ID elements and clear them one by one?
getElementsByClassName returns an array of DOM elements. You need to iterate through them, e.g. via a for loop, and change the innerHTML one by one.
This question already has answers here:
How can I add "href" attribute to a link dynamically using JavaScript?
(9 answers)
Closed 8 years ago.
In my site I have one div like this
<div class="yjme_item_in yjmeitem29">
inside this div there is a link and I would like to replace the href of it, introducing www.google.com.
Any ideas ?
I have tried this with no luck
var a = document.getElementsByClassName("yjmeitem29")[0];
a.setAttribute("href", "www.google.es");
Try this
a.childNodes[0].setAttribute("href", "www.google.es");
This question already has answers here:
Remove all child elements of a DOM node in JavaScript
(37 answers)
Closed 9 years ago.
I found some jQuery code to clear the contents of a div:
$('#masterdiv').empty();
Is there any similar way to do this in raw javascript, without jQuery or any other library?
This code will work.
while (myNode.firstChild) {
myNode.removeChild(myNode.firstChild);
}
Mention the id of the tag on myNode for which you want to remove the inner child.
Please have a look on
http://jsfiddle.net/2dJAN/19/
<div class="btn-post">123</div>
$(".btn-post").html("")
This question already has answers here:
How to get the entire document HTML as a string?
(16 answers)
Closed 5 years ago.
I know there's a way to do this, but I cannot recall how.
How do I go about grabbing the HTML element (top-most element in the DOM), using plain JavaScript?
Use document.documentElement.
See the docs: https://developer.mozilla.org/en-US/docs/Web/API/Document/documentElement
var html = document.getElementsByTagName('html')[0];