views:

87

answers:

4

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?

+1  A: 

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.

Pointy
I started with exactly that. Then it wasn't working and I started second guessing. I realized that it wasn't the selector that I was having issues with! Thanks
Deshiknaves
+4  A: 

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)
GlenCrawford
You're right I am kicking myself. I started with exactly that. Then it wasn't working and I started second guessing. I realized that it wasn't the selector that I was having issues with! Thanks
Deshiknaves
No probs. We all make simple mistakes!
GlenCrawford
+1  A: 

just use $("#show_posts_"+postID)

DCrystal
A: 

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)
Syntactic