Skip to content
Advertisement

How to change the id of an element in html using javascript?

It is my first time using JavaScript. I am trying to make a button where every time visitors click, it’ll show another extra line of text. I often get an error on my JavaScript, and I’m not sure how to fix it. Thank you so much!

HTML;

JavaScript

JS;

JavaScript

Advertisement

Answer

Your code throws error because you are trying to set hr2 and hr3 when they are not exist. You need to set hr2 and hr3 variables after setting id’s of them like below:

JavaScript

JavaScript
JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement