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?
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.
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.
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.
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.
Instead, you can set same class to your controls and hide them like that :
$('.controlClass').hide();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With