views:

115

answers:

2

Hey guys,

I'm trying to find an element using jQuery, but it is not working.. I found out that this kind of selector can't be done in Greasemonkey:

($("#app7019261521_hover_container > [id^=app7019261521_the_coin]"))

Please help me translate this into raw Javascript. This kind of selector is hardcore to do in Javascript. Please help me Javascript gurus!

+2  A: 

This should do it, and now I remember why I started using jQuery:

var children = document.getElementById('app7019261521_hover_container').childNodes;
var ids = []; //to store the IDs of all matching elements
for(var i = 0; i < children.length; i++)
{
    //indexOf returns zero is subject starts with passed string
    if(children.item(i).id.indexOf('app7019261521_the_coin') == 0)
    {
        alert('Got One!');
        ids.push(children.item(i).id);
    }
}
karim79
+1  A: 

Since you are targeting directly to Firefox, you may want to give a look to the Selectors API implemented on Firefox 3.5.

Check the document.querySelectorAll function:

var elements = document.querySelectorAll("#app7019261521_hover_container > [id^=app7019261521_the_coin]")
CMS
I'd be surprised if this works. If jQuery doesn't work then this shouldn't either, afterall, in capable browsers, jQuery (Sizzle) uses document.querySelectorAll...
J-P