views:

114

answers:

3

Hi, I want to scan a website using jQuery, but the ID is constantly changing, but there's a permanent pattern for the ID that I'm searching for:

app7019261521_the_coinb4678bc2
app7019261521_the_coind42fgr23
app7019261521_the_coing0992gvb
app7019261521_the_coin12e5d0aa

The IDs always starts with app7019261521_the_coin

But my problem is I don't know how to put that in jQuery selector.

$("#app7019261521_the_coin")

Doesn't seem to work

So how can I make this work?

+2  A: 

can you set a class and just call it by a class name?

you may also be able to try

$("div[id^=app7019261521_the_coin]")
Jason
why the downvote? is my answer wrong?
Jason
+1  A: 

This will find all div's that start with *app7019261521_the_coin*

Replace div with whatever element type you are searching for.

$j('div[id^=app7019261521_the_coin]')

Remember this is not very optimal, as it causes the script to check the id attribute of every matched element.

You might want to see how you can add a class to the element or at least find the parent element first and traverse from there.

Bryan Migliorisi
-1 as this answer was already provided. Please up-vote others who answer first.
Jonathan Sampson
Downvote? Is it my fault that StackOverflow didnt show me the other answers when I wrote mine? Great having you around, Jonathan.
Bryan Migliorisi
+6  A: 
$("[id^=app7019261521_the_coin]")

Should work - but its MUCH slower selector than knowing the real ID - or assigning a class. This selector will scan every element on the page one at a time, there is no good way for this selector to be optimizied. 9 times out of 10 though you could build a better selector: Is this #app7019... element the direct child of another element that is easier to determine? like a id='container'?

$("#conainter > [id^=app7019261521_the_coin]"); for instance

From the jQuery Selector Documentation

[attribute^=value]    Returns: Array<Element(s)>
Matches elements that have the specified attribute and it starts 
with a certain value.
gnarf
-1 as this answer was already provided. Please up-vote others who answer first.
Jonathan Sampson
@Jonathan, so what? they're not identical answers, don't tell people how to vote
hasen j
cont: for the record, this answer is better because it explains *why* it works. That's probably why it took him a couple more minutes to post it.
hasen j
@jonathan: both the other answers incorrectly assume that the item he is looking for is a div, or that its even needed to specify the tag type - plus my answer was a few minutes later because i went and found the selector documentation and explained what it was doing
gnarf
none of the answers here are incorrect. but i agree that this answer is more explicit. i voted for it and i posted the first response :)
Jason