chappers
chappers

Reputation: 466

Google Tag Manager css selector to get value of third span

How do i get the value of 93 from the third span in this example?

<div class="rating-result">
<span>
<span>
<span itemprop="ratingValue">93</span>% of <span 
itemprop="bestRating">100</span>
</span>
</span>
</div>

Ive tried things like this but neither works:

div.rating-result > span > span or div.rating-result > span:nth-child(3)

Upvotes: 0

Views: 757

Answers (2)

Suresh Ponnukalai
Suresh Ponnukalai

Reputation: 13998

If you are using jquery then you can use the following way to get the text 93.

$("div.rating-result span[itemprop='ratingValue']").text()

or

$("div.rating-result > span > span > span:first-child").text()

console.log($("div.rating-result > span > span > span:first-child").text());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="rating-result">
<span>
<span>
<span itemprop="ratingValue">93</span>% of <span 
itemprop="bestRating">100</span>
</span>
</span>
</div>

Upvotes: 1

abraham63
abraham63

Reputation: 443

Use

var el=document.querySelector('span[itemprop=ratingValue]').textContent;

Your result (93) is in el variable. With tis line your get all span with an attribute itemprop which add value ratingValue. Be carrefull document.querySelector('span[itemprop=ratingValue]') can return only the first occurence.

Upvotes: 1

Related Questions