How to use jquery selector having element type and ID

前端 未结 2 549
一整个雨季
一整个雨季 2021-02-13 05:00

I\'m using this selector $(\"textarea #myTextArea\").val(text); and it\'s not working. If I remove the ID and use the class it\'s working. Why isn\'t jquery able t

相关标签:
2条回答
  • 2021-02-13 05:14

    Because of the space. With the space it says the #myTextArea within a textarea.

    $("textarea#myTextArea").val(text);
    
    0 讨论(0)
  • 2021-02-13 05:18

    Just remove the space:

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

    At the moment you're trying to select an element with ID myTextArea that is a descendant element of a textarea

    As Jared Farrish mentions in the comments removing the element type would be more efficient:

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

    If your document is valid then every ID will only used be once so this is still correct.

    0 讨论(0)
提交回复
热议问题