Find html element which id starts with

Jquery

Jquery Problem Overview


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.

Jquery Solutions


Solution 1 - Jquery

Try

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

It should work

Solution 2 - Jquery

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

$('[id^=list_]').whatever()

Solution 3 - Jquery

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

Solution 4 - Jquery

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.

Solution 5 - Jquery

To get an element ending with a certain id/character

find('[id$=someid]')

To get an element starting with a certain id/character

find('[id*=anotherid]')

To get an element matching with a certain id/character

find('[id^=id]')

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionliderminView Question on Stackoverflow
Solution 1 - JqueryGlen LittleView Answer on Stackoverflow
Solution 2 - JquerySLaksView Answer on Stackoverflow
Solution 3 - JqueryYuriy FaktorovichView Answer on Stackoverflow
Solution 4 - JqueryChetan SView Answer on Stackoverflow
Solution 5 - JqueryHaroon KhalidView Answer on Stackoverflow