Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if id contains multiple words?

I have a div:

<div id="picture_contents_12356_title"></div>

The '12356' is a unique id that is auto-generated and I need to have it there. How can I create a jQuery selector for an id that has "picture_contents" AND "title"?

I know you can do [id*="picture_contents"] but I also need to check if it contains "title" as well. How do I do create the AND logic there?

I was thinking maybe extracting the id into a variable and checking indexOf("picture_contents") and indexOf("title") but I was wondering if there was a better way.

like image 216
Alistair Avatar asked Feb 11 '23 12:02

Alistair


1 Answers

Just keep adding attribute based selectors [attribute*=".."]

In your case,

$('[id*="picture_contents"][id*="title"]')

Suggestion: if the pattern of words is constant, you could use ^= : starts-with or $=: ends-with selector too.


For,

<div id="picture_contents_12356_title"></div>

Use,

$('[id^="picture_contents"][id$="title"]')
like image 164
Shaunak D Avatar answered Feb 13 '23 00:02

Shaunak D