Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery wild card character

I have 3 controls with id control_1, control_2, control_3.

I want to hide these controls.

Currently I am using this:

$('#control_1').hide();
$('#control_2').hide();
$('#control_3').hide();

Is there a better way of doing this?

Can I do something like $('control_*').hide();?

Is there a way to find controls with start with a specific name?

like image 656
Balaji Avatar asked Sep 07 '09 11:09

Balaji


People also ask

How to use wildcard in jQuery?

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 * In wildcard selector?

It selects similar type of class name or attribute and use CSS property. * wildcard also known as containing wildcard. [attribute*=”str”] Selector: The [attribute*=”str”] selector is used to select that elements whose attribute value contains the specified sub string str.

Can we use multiple selectors in jQuery?

You can specify any number of selectors to combine into a single result. This multiple expression combinator is an efficient way to select disparate elements. The order of the DOM elements in the returned jQuery object may not be identical, as they will be in document order.


2 Answers

For completeness, you can use the starts with attribute filter:

$('[id^="control_"]').hide();

That said, for most purposes it would be better to go with one of the other suggestions.

like image 167
karim79 Avatar answered Sep 28 '22 06:09

karim79


Instead, you can set same class to your controls and hide them like that :

$('.controlClass').hide();
like image 39
Canavar Avatar answered Sep 28 '22 07:09

Canavar