How to use jquery selector with element type and id

I use this $("textarea #myTextArea").val(text); selector $("textarea #myTextArea").val(text); and it does not work. If I remove the identifier and use the class that it works. Why can't jquery find an element here?

+7
source share
2 answers

Because of the space. With space, he says #myTextArea within textarea .

 $("textarea#myTextArea").val(text); 
+27
source

Just remove the space:

 $("textarea#myTextArea").val(text); 

You are currently trying to select an element with the identifier myTextArea , which is a descendant of the textarea element

As Jared Farrish says in the comments, removing an item type would be more efficient:

 $("#myTextArea").val(text); 

If your document is valid, each identifier will be used only once, so this is still correct.

+4
source

All Articles