How do I select text nodes with jQuery?
jQuery doesn't have a convenient function for this. You need to combine contents()
, which will give just child nodes but includes text nodes, with find()
, which gives all descendant elements but no text nodes. Here's what I've come up with:
var getTextNodesIn = function(el) {
return $(el).find(":not(iframe)").addBack().contents().filter(function() {
return this.nodeType == 3;
});
};
getTextNodesIn(el);
Note: If you're using jQuery 1.7 or earlier, the code above will not work. To fix this, replace addBack()
with andSelf()
. andSelf()
is deprecated in favour of addBack()
from 1.8 onwards.
This is somewhat inefficient compared to pure DOM methods and has to include an ugly workaround for jQuery's overloading of its contents()
function (thanks to @rabidsnail in the comments for pointing that out), so here is non-jQuery solution using a simple recursive function. The includeWhitespaceNodes
parameter controls whether or not whitespace text nodes are included in the output (in jQuery they are automatically filtered out).
Update: Fixed bug when includeWhitespaceNodes is falsy.
function getTextNodesIn(node, includeWhitespaceNodes) {
var textNodes = [], nonWhitespaceMatcher = /\S/;
function getTextNodes(node) {
if (node.nodeType == 3) {
if (includeWhitespaceNodes || nonWhitespaceMatcher.test(node.nodeValue)) {
textNodes.push(node);
}
} else {
for (var i = 0, len = node.childNodes.length; i < len; ++i) {
getTextNodes(node.childNodes[i]);
}
}
}
getTextNodes(node);
return textNodes;
}
getTextNodesIn(el);
Get all text nodes using jquery
This is the same question
try this:
$(elem)
.contents()
.filter(function() {
return this.nodeType === 3; //Node.TEXT_NODE
});
jQuery select text nodes and delete their content
As I understood it, I think this is what you are looking for:
$('.example').each(function(){
console.log($(this).contents());
$(this).contents().filter(function(){
if( this.nodeType === 1 && this.childElementCount == 0 && this.textContent == ''){
this.textContent = 'new value';
}
});
});
You can check fiddle
How to select all text nodes after specific element
You can get the content and use split with hr
to get the html after the hr
and then replace this content within a div
and you will be able to manipulate this div
to get your content:
var content = document.querySelector('.someclass').innerHTML;
content = content.split('<hr>');
content = content[1];
document.querySelector('.hide').innerHTML = content;
/**/
var nodes = document.querySelector('.hide').childNodes;
for (var i = 0; i < nodes.length; i++) {
console.log(nodes[i].textContent);
}
.hide {
display: none;
}
<div class="someclass">
<h3>First</h3>
<strong>Second</strong>
<hr> Third
<br> Fourth
<br>
<em></em> ...
</div>
<div class="hide"></div>
jQuery select and wrap textNode
You can use contents
, and filter by node type (3 is for text node):
$('div').contents()
.filter(function(){return this.nodeType === 3})
.wrap('<b />');
Example: http://jsfiddle.net/nJqKq/8
See also: Node Types, at MDC
Related Topics
How to Access the Contents of an Iframe With JavaScript/Jquery
Is There an "Exists" Function For Jquery
Jquery: Return Data After Ajax Call Success
Listening For Variable Changes in JavaScript
Remove Accents/Diacritics in a String in JavaScript
How to Detect Idle Time in JavaScript
Explanation of 'Let' and Block Scoping With For Loops
Generate Unique Random Numbers Between 1 and 100
How to Convert Decimal to Hexadecimal in JavaScript
Why Is Settimeout(Fn, 0) Sometimes Useful
Window.Onload VS $(Document).Ready()
When Should I Use Double or Single Quotes in JavaScript
Client on Node.Js: Uncaught Referenceerror: Require Is Not Defined
How to Use Formdata For Ajax File Upload
Get JavaScript Object from Array of Objects by Value of Property
Is There a "Null Coalescing" Operator in JavaScript
Choosing and Activating the Right Controls on an Ajax-Driven Site