Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add string to jQuery selector?

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 replaced with a number. In the function I need to call $('show_posts_XXXXXX') - XXXXXX being the generated ID. I have stored this ID in a variable called postId.

But I can't seem to do this $("'" + "show_posts_" + postId + "'")

Can anyone tell me how I can add a string to the end of a selector?

like image 767
Deshiknaves Avatar asked Apr 27 '10 11:04

Deshiknaves


Video Answer


2 Answers

Should work. If it does, you'll kick yourself. Don't forget the hash for the ID, and the extra quotation marks aren't necessary.

$("#show_posts_" + postId)
like image 122
GlenCrawford Avatar answered Oct 20 '22 12:10

GlenCrawford


You need to include the '#' character at the start of the string.

$('#show_posts_' + postId)

Also, you're trying to stuff the quotes in there in your example, and that doesn't make sense.

like image 41
Pointy Avatar answered Oct 20 '22 11:10

Pointy