Jquery check element exists by data attribute matching only the start of a specific value [duplicate]

StackOverflow https://stackoverflow.com/questions/23344485

Question

I want to be able to check to see if a specific data attribute exists that starts with a specific value.

for example lets say i have the following HTML code:

 <div data-attr="123ABC"></div>
 <div data-attr="123456"><div>
 <div data-attr="TEST"></div> 

and then i want to find any use of the data tag of data-attr that starts with 123

 if ($('[data-attr="123"]').length > 0) { 
     //do something
 }

This appears to always return 0 as i have no data-attr="123" How would i make it so i can check for only that specific data attribute that starts with 123 and has anything after that?

Was it helpful?

Solution

here is how

if ($('[data-attr^="123"]').length > 0) { 
 //do something
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top