Change Text Inside an Element Using jQuery- why?

I have a question. I could guess the answer myself but would rather get some feedback from you guys!

I’ve just passed the “Change Text Inside an Element Using jQuery” bit, and I’m wondering why you would chose to change the HTML this way rater than just change the code in HTML?

1 Like

Hey, with Javascript (jQuery) you can dynamically change the content. Text that isn’t going to change should not be set with JS. But if you want to change text when a button is clicked (or any other possible event where you want to change text) you will have to use JS to achieve that.

2 Likes

HTML cannot be edited while the page is going. Say you want to send a message to a user, you cannot just change the html, you need to use js to interact with them - it is sooo much more practical.

1 Like

Try to make a simple webpage with only an input field, a submit button and an empty div.
The user will enter his/her name and submit by pressing the button. After that he should see a message like that:
“Hello, user!” while instead of user he’ll see his name.
In order to do that you should change the text inside the div of the “hello, user!” message.
So you’ll do that by getting the input field text with jquery and change the div element to match the user name.
You cannot edit the HTML to fit the user name because it should be changed dynamically.

2 Likes

Aha! Understood. This is where I struggle with this copy and code style learning, because it does not explain very well how it is actually applied in real life sites! It’s all very static. Very helpful answer thank you.

3 Likes

You are right, you will never learn anything just copy and pasting the only way to learn is to apply it with the projects, then stuff like this will become very clear!

I was wondering the exact same question. Now it makes sense.