Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get innerHTML from selection

I have the following divs in an HTML page:

<div class="foo">Bar</div>
<div class="foo">Baz</div>

I'd like to get an array with the text contained in the two divs:

['Bar', 'Baz']

I can do this using d3.nodes but it seems a bit clunky. Is there a smarter way?

d3.selectAll(".foo").nodes().map(function(d) { return d.innerHTML; });

The answer could be pure Javascript, of course!

like image 824
LondonRob Avatar asked Jan 15 '17 20:01

LondonRob


1 Answers

var text = [];
document.querySelectorAll(".foo").forEach(function(e){
    text.push(e.textContent);
});
like image 118
ibrahim mahrir Avatar answered Oct 13 '22 02:10

ibrahim mahrir