-1

i tried changing html tag content but it not working

document.getElementsByTagName('h4').innerHTML('Hi world');
h4{
color:red;
}
<h4>Hello world</h4>
  • 3
    document.getElementsByTagName('h4')[0].innerHTML = 'Hi world'; –  Mar 02 '21 at 09:33
  • 1
    Also, `getElementsByTagName(...)` returns an array. You must choose the element in the array you want to change innerHTML to (element on index 0 in this case) – Cristian Sarghe Mar 02 '21 at 09:35

3 Answers3

0

You're just using the innerHTML property the wrong way.

The right method is .innerHTML = 'Hi world';

Read more about innerHTML here: https://www.w3schools.com/jsref/prop_html_innerhtml.asp

Also, don't forget to select the first index of the array as the getElementsByTagName function returns an array of elements

document.getElementsByTagName('h4')[0]

document.getElementsByTagName('h4')[0].innerHTML='Hi world';
h4{
color:red;
}
<h4>Hello world</h4>
xTrimy
  • 804
  • 9
  • 23
0

It says "getElements" so it's PLURAL. It's a collection of elements so you need to specify which to operate on and hence the square brackets...

document.getElementsByTagName('h4')[0].innerHTML='Hi world';
0

document.getElementsByTagName('h4')[0].innerHTML = 'Hi world';
h4{
color:red;
}
<h4>Hello world</h4>