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>
Related
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
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:
Event binding on dynamically created elements?
(23 answers)
Closed 8 years ago.
jQuery does not seem to be able to select a loaded HTML element. Here is my HTML:
<div class="class name" style="display: none;">
<div id="submenuID" class="submenuID" />
<script>
loadHtmlUsingJavascript('parameter1', 'parameter2');
</script>
</div>
The loaded HTML is a list of links. This is my JavaScript:
$("#submenuID li").addClass("active");
But it would not add the class. After some snooping around. I learn to use
$("#submenuID li").live('click', function() {
$("#submenuID li").addClass("active");
});
However, it does not work until I click on the link a second time.
You have to do the following:
$("#submenuID").on('click', 'li', function() {
$(this).addClass("active");
});
First, the jQuery .live function is deprecated since jQuery 1.7 (http://api.jquery.com/live/)
Also, you should be listening for li methods inside the #subMenuID element.
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'));
This question already has answers here:
Jquery change text between two elements
(9 answers)
Closed 9 years ago.
I have a html content as shown below.Is there any way to obtain using Jquery the text in between the two anchor tags without wrapping any div or span tags around the text named "user1" .I need the output as "user1". Could someone please help me.
<div class="test 1">
<input id="field1" type="hidden" value="terminal">
<a class="Prev" title="prevoius" href="#">previous</a>
User1
<a class="btnNext" title="next" href="#">next</a>
</div>
Something like this should work:
var theText = $('.Prev')[0].nextSibling.textContent || $('.Prev')[0].nextSibling.innerText;
Here's a fiddle
Following code would give you the desired text.
var ch1 = $("a[class=Prev]");
var ch2 = $("a[class=btnNext]");
var contents = ch1.parent().contents();
contents.slice(contents.index(ch1) + 1, contents.index(ch2)).text();
Try this: http://api.jquery.com/contents/
contents returns the "children of each element in the set of matched elements, including text and comment nodes". It should return four children: input, a, text, a.
You can do it with nextUntil.
http://api.jquery.com/nextUntil/