Skip to content
Advertisement

Switch paragraphs using inner HTML in JS

I am trying to switch two paragraphs after clicking the button but I am stuck. Is there any way how to do this using inner HTML without using IF or boolean? I tried this code but it doesn’t work. Thanks

JavaScript

Advertisement

Answer

Why don’t you use querySelector in place of using querySelectorAll and choose the first element?

By the way, I advise to delete and re-add the elements from the parent rather than using innerHTML. The use of innerHTML would not preserve listeners and have worse performances:

JavaScript

Example:

JavaScript
JavaScript
Advertisement