This question already has answers here:
Remove all child elements of a DOM node in JavaScript
(37 answers)
How do I clear the content of a div using JavaScript? [closed]
(2 answers)
Closed 5 years ago.
I want to remove the everything under the classname: social-share using JavaScript
HTML: This is what it looks like when I grab the code from chrome inspector.
<div class="social-share">
[shareaholic app="share_buttons" id="26444890"]
</div>
HTML: This is what exactly looks like inside the chrome inspector
<div class="social-share">
"
[shareaholic app="share_buttons" id="26444890"]
" == $0
</div>
You can use innerHTML to clear the contents of the element after it has been selected:
// select element with class 'social-share:
var social_share = document.querySelector('.social-share');
// Set it's contents to an empty string
social_share.innerHTML = '';
$('.social-share').empty();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="social-share"> content
<div>content</div>
</div>
Jquery empty() might be help you. It will remove every thing inside social-share class
Related
This question already has answers here:
Parse an HTML string with JS
(15 answers)
Remove all text content from HTML div but keep HTML tags and structure
(8 answers)
Get the string representation of a DOM node
(13 answers)
I need help using recursion to navigate every element in the DOM
(2 answers)
Inserting arbitrary HTML into a DocumentFragment
(11 answers)
Closed 26 days ago.
I'm using javascript to scrape data. I want to scrape just html-tags and their attributes (everything between <> )without content.
like for example given:
<h1>My First Heading</h1>
<div>
<p>My first paragraph.</p>
</div>
I would extract the following:
<h1> </h1>
<div>
<p> </p>
</div>
Any way to do that?
Thanks in advance!
Regular expression looks but this solution sounds naive.
This question already has answers here:
Can you do an 'AND' on a jQuery selector to target multiple buttons?
(3 answers)
Closed 4 months ago.
I'm a beginner with JavaScript so I'm sure this will be an elementary question.
With this function below, I want it to find the two elements and add them to a header element. Right now, the only thing that is working is the element, the link with the rel:author tag. I'm sure this is probably a syntax thing.
(function($) {
$(document).ready(function() {
$(".single .single-post-module").each(function() {
$(this).find('a[rel="author"]', 'span[class="updated"]').clone().appendTo($(this).find('.post-header h1'));
// ^^^
});
});
})(jQuery)
Edit: I'm using Divi on wordpress and the site is in maintenance mode. That's why didn't show the HTML or give a link.
This could be done with pure Javscript querySelectorAll with an CSS selector that will find both elements.
To clone the element, use cloneNode(true) where the first parameter indicates to clone 'deep'
const header = document.querySelector('.post-header > h1');
const elements = document.querySelectorAll('.single, .single-post-module');
Array.from(elements).forEach(e => header.append(e.cloneNode(true)));
<div class='single'>
<a rel='author'>Link</a>
<span class='updated'>Span</span>
</div>
<div class='single-post-module'>
<a rel='author'>Link</a>
<span class='updated'>Span</span>
</div>
<div class='post-header'>
<h1></h1>
</div>
This question already has answers here:
jQuery: How to get the value of an html attribute?
(6 answers)
Closed 7 years ago.
My my example below I've created an attribute called 'filters'. How can i get the attributes values using jquery? which would be games, vfx, video?
<div class="portfolio-item" filters="games vfx video">
<p>This is a test</p>
</div>
Just do :
$('.portfolio-item') // use class selector to target the class 'portfolio-item'
.attr('filters'); // This would give you the value of the attribute 'filters'
As pointed by others, you should use data attributes ( as supported by jquery 1.10 or higher ) for consistency with custom attributes:
<div class="portfolio-item" data-filters="games vfx video">
and extract its value : $('.portfolio-item').data('filters');
This question already has answers here:
Selecting element by data attribute with jQuery
(12 answers)
Closed 8 years ago.
Assuming i have a dvMainDiv as shown bellow with multiple children of type divs aswel. Now in my software the children have been added programmatically. Assuming the html is as shown below, how do get a child dive with a specific data-id.
<div id="dvMainDiv" class="dvMainDiv">
<div id="dvChildDiv" class="dvChildDiv" data-id="1">
<p>
some text
<p>
</div>
<div id="dvChildDiv" class="dvChildDiv" data-id="2">
<p>
some text 2
<p>
</div>
</div>
Say for instance, i want to get only the div with data-id of 1 and not 2, how do i do this in jquery.
for your code you would use the following:
$('*[data-id="1"]');
See http://jsfiddle.net/bgqd8xe6/
Edit to use a variable for the data-id use:
$('*[data-id="' + test + '1"]');
Note test must be a string
This question already has answers here:
Get selected element's outer HTML
(30 answers)
Closed 8 years ago.
Hello everyone,
I'm a little bit confused here, trying to store couple of html elements into a variable.
But when I try to print out the content of my variable, some elements are not being displayed. is that some kind of magic :) or what I'm doing wrong exactly?
This is then causing issues in my other functions, which are trying to find certain elements in the variable.
var template = $(" <div class='item'><span id='item_stats_info'> <div id='item_name'> </div> </span> </div>");
console.log(template.html());
Result:
<span id="item_stats_info"> <div id="item_name"> </div> </span>
Could you guys advise me on what's the best way to store html elements in a variable?
Thansk in advance,
Alex
The .html() method of jQuery returns the HTML inside of the element, since you need to get the outer HTML, you can try wrapping it in another div:
var template = $("<div> <div class='item'><span id='item_stats_info'> <div id='item_name'> </div> </span> </div> </div>");
console.log(template.html());
Use this to get the full html, not the inner html....
console.log(template.prop('outerHTML'));