0

I have the following script:

<div>
  <p id="example1"></p>
  <div></div>
  <p id="example2"></p>
  <div></div>
  <p id="example3"></p>
  <p id="example4"></p>
  <p id="example5"></p>
</div>

Is it possible to write a jquery function to determine the total number of element id with matching this keyword "example" when the page is loaded?

Thanks in advance!

user610983
  • 855
  • 2
  • 9
  • 14
  • possible duplicate of [JQuery selector regular expressions](http://stackoverflow.com/questions/190253/jquery-selector-regular-expressions) – Galen Apr 21 '11 at 03:35
  • exact duplicate of http://stackoverflow.com/questions/1225611/jquery-how-to-find-list-of-divs-with-similar-id – EdmundYeung99 Apr 21 '11 at 03:52

3 Answers3

2

Check-it Out:

alert($("div p[id^='example']").length);

CLICK HERE TO SEE THE DEMO

OR

refer this link JQuery Selectors

where u can use selectors as per your need.

Sukhjeevan
  • 3,074
  • 9
  • 46
  • 89
1

Sukhi answered you correctly.

Code fragment: p[id^='emample'] implies any p "starts with" the "example" word in id attribute.

But what if you want to check "ends with" condition? Simply replace ^ with $, so your code will be somthing like this: p[id$='1'] to select any id which has "1" at the end.

seoul
  • 864
  • 1
  • 12
  • 32
0

Use the attribute contains selector functionality:

$('p[id*="example"]').length;
notbrain
  • 3,366
  • 2
  • 32
  • 42