+ 7
[SOLVED] Js change text of first child help
In lection they show us how to change text of all child, but whan i try to change text of just one child it dont work. Why dont work and how we can change text of one child? https://code.sololearn.com/WBikLiaC42cB/?ref=app
6 odpowiedzi
+ 12
Change a.first Child in line 3 in JS to a.firstElementChild
Code:
function setText() {
var a = document.getElementById("demo");
var arr = a.firstElementChild;
arr.innerHTML = "new text";
}
+ 6
VEDANG Many thanks 😊
Also why in lection is element.firstChild??
I was follow their path and writte a.firstChild
https://www.sololearn.com/learn/JavaScript/2753/
+ 5
Calviղ Is * to select all child of that id? And than with index you choose just first?
+ 3
nice answer Vedang.
if you wonder why in the method name, there is an `Element`,
here is a supplement:
https://code.sololearn.com/Ws1O44AwLDMW/?ref=app
+ 3
Sanja Panic VEDANG
firstChild and firstElementChild cannot be used in update element. They are read only properties only return the element text with innerText method.
Use querySelector to get all child elements and update the first first with array index 0
function setText() {
var a = document.querySelectorAll("#demo *")[0];
a.innerHTML = "new text";
}
https://code.sololearn.com/W1AT2EcrUo3Q/?ref=app
+ 2
Sanja Panic you're right, it follow css syntax the same way to select elements.