Reputation: 437
In the html I am working with there is this approximate structure:
...
<span class="priceValue">
...
<span id="asd...">
12345
</span>
</span>
...
I want to get the number that is the *.text() of the second (the nested one). The problem is that I cannot use the "id=" property.
Any ideas for the jQuery code needed?
Upvotes: 0
Views: 77
Reputation: 1075765
If it's the first span
within the first .priceValue
element, then:
var text = $(".priceValue > span").first().text();
var text = $(".priceValue > span").first().text();
snippet.log(text);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="priceValue">
<div>Not me</div>
<span>One</span>
<span>Not me</span>
</div>
<div class="priceValue">
<div>Not me</div>
<span>Two</span>
<span>Not me</span>
</div>
<hr>
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
If you have several .priceValue
elements and need the text of the first span inside each of them, it's a bit trickier:
var textArray = $(".priceValue").map(function() {
return $(this).children("span").first().text();
}).get();
var textArray = $(".priceValue").map(function() {
return $(this).children("span").first().text();
}).get();
snippet.log(textArray.join(", "));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="priceValue">
<div>Not me</div>
<span>One</span>
<span>Not me</span>
</div>
<div class="priceValue">
<div>Not me</div>
<span>Two</span>
<span>Not me</span>
</div>
<hr>
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
Upvotes: 2
Reputation: 829
$("span>span").text(); // direct descendant use this
$("span span").text(); // if don't know if next span is direct descendant
Upvotes: 0