+ 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

10th Jul 2019, 9:51 AM
PanicS
PanicS - avatar
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"; }
10th Jul 2019, 10:00 AM
VEDANG
VEDANG - avatar
+ 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/
10th Jul 2019, 12:46 PM
PanicS
PanicS - avatar
+ 5
Calviղ Is * to select all child of that id? And than with index you choose just first?
10th Jul 2019, 1:31 PM
PanicS
PanicS - avatar
+ 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
10th Jul 2019, 11:47 AM
Gordon
Gordon - avatar
+ 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
10th Jul 2019, 1:15 PM
Calviղ
Calviղ - avatar
+ 2
Sanja Panic you're right, it follow css syntax the same way to select elements.
10th Jul 2019, 1:35 PM
Calviղ
Calviղ - avatar