kevin
kevin

Reputation: 33

How to get text value by span using javascript

I want to get text value "user detail - 205 users" by the span , but i can not change the code by adding id or class , can i use javascript to get this ?

<span style="color: rgb(0, 189, 12);">"user detail - 205 users"</span>

Upvotes: 1

Views: 135

Answers (1)

charlietfl
charlietfl

Reputation: 171690

Assuming the text "user detail" always exists you can find the span that includes that text and get the full text once found

const spans = [...document.querySelectorAll('span')],
      wantedSpan = spans.find(el => el.textContent.includes('user detail')),
      wantedText = wantedSpan ? wantedSpan.textContent : 'Not Found';

console.log('wantedText:', wantedText)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<span style="color: rgb(0, 189, 12);">"user detail - 205 users"</span>

Upvotes: 1

Related Questions