I’m piggy-backing on this link…
Extract the Text in a Element with JQuery
I like to extract the text in a element then append it to an attribute like so..
jQuery
$(document).ready(function() {
var text;
$(".parent span").contents().each(function(i) {
if(this.nodeName == "#text") text = $(this).text();
$(".child").attr("ref", text);
});
});
HTML
<div class="parent">
<div class="child"> </div>
<span><strong>bla bla bla</strong>Child-1</span> </div>
<div class="parent">
<div class="child"> </div>
<span><strong>bla bla bla</strong>Child-2</span>
</div>
I keep getting “Child-1” in each parent’s ref.
Try this:
Note: I used the accepted answer in the linked question for obtaining the desired text from the first
div.