How to add string to jQuery selector?

后端 未结 4 1751
半阙折子戏
半阙折子戏 2020-12-19 06:02

I\'m trying to select an id that changes on different posts of same page. So the they have been given an id=\"show_posts_{PostID}\" - on the final output the {PostID} is re

4条回答
  •  一生所求
    2020-12-19 06:22

    Two things.

    1. To select an element with a given ID, you need a # character at the beginning of the ID.

    2. Don't add the quotes to the beginning and end of the string.

    Thus:

    $('#show_posts_' + postId)
    

提交回复
热议问题