Change Text Inside an Element Using jQuery- why?

Change Text Inside an Element Using jQuery- why?
0.0 0

#1

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?


#2

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.


#3

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.


#4

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.


#5

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.


#6

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!


#7

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