Change Text Using JS querySelector

This is my 2nd post for the daily-post project. Today I learned a bit about querySelector in JS. I suggest you copy paste the following code in your Sublime Text and save it as your-desired-filename.html on desktop or your sandbox folder.

This is such a simple trick to turn your static page into a dynamic one!

  1. We basically write `I love Batman` text inside the paragraph tag p.
  2. Then use the querySelector to select the first occurrence of p tag in the source code.
  3. Save that as reference in variable t.
  4. Add an event listener to it which will trigger myFunction when clicked
  5. myFunction will prompt us to enter a name and then modify the content inside the variable t.

That’s all. Simple but useful. Expect more JS in my future posts 🙂