Jake Jake - 4 months ago 10
Javascript Question

Inner text from selector

I tried to take the inner text from a selector like this

<span ng-if="vm.totalRecords > 1" class="ng-binding ng-scope">Displaying results 1-25 of 17,430</span>


using this

document.querySelectorAll('div.Dashboard-section div.pure-u-1-1 span.ng-binding.ng-scope').innerText


but it gives back
undefined
. Any suggestions what am I doing wrong?

Answer

You need to do the following:

console.log(document.querySelectorAll('span')[0].innerText);
<span ng-if="vm.totalRecords > 1" class="ng-binding ng-scope">Displaying results 1-25 of 17,430</span>

This will log the innerText as opposed to the other code due to two reasons:

  1. Using querySelectorAll returns a list of results, hence why we use [0] to reference the first found element.

  2. The actual query from your querySelectorAll couldn't find the HTML element. Simply using span as the query is enough.