This is my code:
<p >
<span >text1</span>
<span>text2</span>
<span>text3</span>
</p>
<p >
<span >text1</span>
<span>text2</span>
<span>text3</span>
</p>
and each other 3 <p>
tags identical.
This my jquery
var promo = $('[class^="myclass"]').map(function() {
return $(this).text();
//I try something like return $(this).children.text(':first') but NOT Work
}).get();
let promo1 = promo[0];
let promo2 = promo[1];
let promo3 = promo[2];
Results: promo1=Text Text2 text3
How can I get the text from only ROWN 1 (Text) and not the other span tag content?
tks
CodePudding user response:
var promo = $('[class^="myclass"]').map(function() {
return $(this).children(":first").html();
}).get();
let promo1 = promo[0];
let promo2 = promo[1];
let promo3 = promo[2];
CodePudding user response:
try this code to get the value from the first <span>
var promo = $('[class^="myclass"]').map(function() {
return $(this).find("span:first-of-type").text();
}).get();