Reputation: 18278
Given a HTML DOM element with b
elements such :
<div>
My <b>text</b> have some <b>words</b> or <b>groups of words</b> in <b>bold</b>. How to do a variable <b>list</b> with these texts ?
</div>
Given we attack this with JS or JQuery.
How to get the list of all bold words, something like such :
var list = [ "text", "have some ", "words", "groups of words", "in","bold", "list" ,...];
I tried .text()
and .html()
but it doesn't work, see http://jsfiddle.net/LF5dX/1/
JSfiddle demo appreciate since more relevant for all future readers / users.
Upvotes: 0
Views: 103
Reputation:
EDIT: Oh, ok, I see OP has edited the answer, so this answer is no longer on-topic. Leaving it here though.
Not sure how you'd do it in jQuery, but here's how to do it in plain JavaScript:
var div = document.getElementsByTagName('div')[0];
var textContents = [].map.call(div.childNodes, function(node) {
return node.textContent.trim();
});
console.log(textContents);
Upvotes: 0
Reputation: 388406
You can use .map() along with .text() and .get()
var list = $('div b').map(function(){
return $.trim($(this).text());
}).get()
Demo: Fiddle
Upvotes: 5
Reputation: 40639
Try this,
var arr=[];
$('div b').each(function(){
arr.push($(this).text());
});
console.log(arr);
Upvotes: 2