How to use jquery selector having element type and ID

前端 未结 2 586
一整个雨季
一整个雨季 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: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.

提交回复
热议问题