tags:

views:

143

answers:

4

Hi, my question is this:

I have HTML code in multiple pages, on each of them I used a JQgrid (jquery grid) for display some data. I knew that on each of those pages, the element that holds the JQgrid is named as "LIST_xxx". Now I need to make a javascript that takes that element "LIST_XXXX" on each page and does some stuff. How could I search for an element by ID but only knowing its initial part (of the ID ,like i mentioned previously):

$('#list_[XXXX]')... --> The part surrounded by [] is variable on each page, i want to discriminate that.

I hope i made myself clear. Thanks.

+5  A: 

Give that element a common class name or some other attribute you can query for.

Yuriy Faktorovich
Hi, thanks for your answer, the problem with your suggestion is that there might be multiple tables holding JQgrid, so there could be: "LIST_customer", "LIST_carrier"... and so on. And I need to select both of them basen only on the initial ID part: "LIST_".
lidermin
I understand, but making them all a List class seems like a more readable and descriptive approach to me. To each his own, you could always use the starts with selector in jquery as shown by other answers.
Yuriy Faktorovich
@lidermin What Yuriy says should work. Why dont you add a class to your controls such as "list" since you have list_ as your prefix. so when you select that class it will get each of those controls. This will be the most efficient way of doing it.
jmein
Yuriy's answer will work. You can add the same class (for example "ListsINeedToFind") to all your "LIST_XXXX" elements, and then look them up using `$('.ListsINeedToFind')`.
Annabelle
+2  A: 

You need to use the attribute starts with selector, like this:

$('[id^=list_]').whatever()
SLaks
+7  A: 

Try

$('div[id^="list_"]')

A useful reference (although a bit dated now) for JQuery is at VisualJQuery

Glen Little
Much better layout on that reference than the jquery documentation.
Yuriy Faktorovich
+1  A: 

Use the "attribute starts with" selector http://api.jquery.com/attribute-starts-with-selector/

$("[id^=list_]")

Be aware that this is inefficient. Prefix with the tag name and descend from the nearest parent if possible.

Chetan Sastry