Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change text of nodelist

When I use this code in the console:

document.querySelectorAll("a.pointer[title='Average']")

It returns a list of Averages, each of which displays text on the page:

<a class="pointer" title="Average" onclick="showScoretab(this)">421.95</a>
<a class="pointer" title="Average" onclick="showScoretab(this)">225.02</a>
<a class="pointer" title="Average" onclick="showScoretab(this)">292.51</a>

How would I go about changing the text of all of these to "0"? I've already tried:

document.querySelectorAll("a.pointer[title='Average']").textContent = "0";
document.querySelectorAll("a.pointer[title='Average']").innerHTML = "0";
document.querySelectorAll("a.pointer[title='Average']").text = "0";
like image 257
Omega Avatar asked Feb 09 '17 16:02

Omega


People also ask

What is the difference between NodeList and HTMLCollection?

Differences Between an HTML Collection and a NodeList An HTML Collection does not have access to array methods, despite it looking like an array. A NodeList has access to the forEach array method, which is useful when you want to iterate through the nodes.

Is NodeList same as an array?

childNodes and methods such as document. querySelectorAll() . Note: Although NodeList is not an Array , it is possible to iterate over it with forEach() . It can also be converted to a real Array using Array.

How do I access NodeList elements?

NodeList items can only be accessed by their index number. An HTMLCollection is always a live collection. Example: If you add a <li> element to a list in the DOM, the list in the HTMLCollection will also change. A NodeList is most often a static collection.

What is a static NodeList?

The document. querySelectorAll() returns a static NodeList. That means that it doesn't change, even if the UI does.


3 Answers

You have to use loop for this purpose, for example using for loop :

var links = document.querySelectorAll("a.pointer[title='Average']");

for (var i=0; i < links.length; i++) {
    links[i].innerHTML = '0';
}
<a class="pointer" title="Average" onclick="showScoretab(this)">421.95</a><br>
<a class="pointer" title="Average" onclick="showScoretab(this)">225.02</a><br>
<a class="pointer" title="Average" onclick="showScoretab(this)">292.51</a>

But since your using jQuery that could be simply like :

$("a.pointer[title='Average']").text('0');

Hope this helps.

$("a.pointer[title='Average']").text('0');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<a class="pointer" title="Average" onclick="showScoretab(this)">421.95</a>
<br>
<a class="pointer" title="Average" onclick="showScoretab(this)">225.02</a>
<br>
<a class="pointer" title="Average" onclick="showScoretab(this)">292.51</a>
like image 60
Zakaria Acharki Avatar answered Nov 01 '22 23:11

Zakaria Acharki


document.querySelectorAll returns a nodelist, not just a single object. Just iterate through each one and set the properties on each object:

var objects = document.querySelectorAll("a.pointer[title='Average']");
for (var i in objects) {
  object[i].innerHTML = "0";
}
like image 44
Matt Spinks Avatar answered Nov 01 '22 22:11

Matt Spinks


Get the total length of the <a class="pointer" title="Average" onclick="showScoretab(this)"> and do a for loop, and inside the loop, that's where you set the innerHTML of the elements into 0

var pointers = document.querySelectorAll("a.pointer[title='Average']");

for(var i = 0; i < pointers.length; i++){
  pointers[i].innerHTML = "0";
}
<a class="pointer" title="Average" onclick="showScoretab(this)">421.95</a>
<a class="pointer" title="Average" onclick="showScoretab(this)">225.02</a>
<a class="pointer" title="Average" onclick="showScoretab(this)">292.51</a>
like image 23
prtdomingo Avatar answered Nov 01 '22 21:11

prtdomingo