Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wildcards in jQuery selectors

I'm trying to use a wildcard to get the id of all the elements whose id begin with "jander". I tried $('#jander*'), $('#jander%') but it doesn't work..

I know I can use classes of the elements to solve it, but it is also possible using wildcards??

<script type="text/javascript">    var prueba = [];    $('#jander').each(function () {     prueba.push($(this).attr('id'));   });    alert(prueba);   });  </script>  <div id="jander1"></div> <div id="jander2"></div> 
like image 958
ziiweb Avatar asked Mar 21 '11 10:03

ziiweb


People also ask

How to use wildcard in jQuery selector?

For getting the id that begins or ends with a particular string in jQuery selectors, you shouldn't use the wildcards $('#name*'), $('#name%'). Instead use the characters ^and $. The ^ is used is used to get all elements starting with a particular string.

What is the purpose of wildcard in selectors?

Wildcards are symbols that enable you to replace zero or multiple characters in a string. These can be quite useful when dealing with dynamically-changing attributes in a selector.


1 Answers

To get all the elements starting with "jander" you should use:

$("[id^=jander]") 

To get those that end with "jander"

$("[id$=jander]") 

See also the JQuery documentation

like image 165
nico Avatar answered Oct 10 '22 21:10

nico