Why does val() work, but html() and text() don't?


#1

Hello staff. I have an issue with the same exercise.

I have actually completed it without problems after seeing this code, but i was wandering why the code i wrote before didn't work.

Basically what i was trying to do was to use html(), text() or empty() on the "#comment" to delete it, but it just didn't work. Why is this, and why does the val() code work?

I've completed the exercise, I'm asking to understand the problem and for future reference. Thank for your help.


THREADLY - set value to an empty string
#2

@daiquiriism A quick search online for text() versus val() should help you find this out :slightly_smiling:

html() and text() control the inside of an element. As you type inside an <input> or <textarea>, it's contents inside it in the HTML aren't changing. Only what you see on your screen is changing, so anything meant to control the inside HTML of an element won't work. But val() is meant specifically for <input>s and <textarea>s (and nothing else), so it works :slightly_smiling: