Balaji
Balaji

Reputation: 2189

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?

Upvotes: 5

Views: 9770

Answers (4)

Bavo
Bavo

Reputation: 431

You could use:

$('#control_1,#control_2,#control3').hide();

or use attributeStartsWith

Upvotes: 2

karim79
karim79

Reputation: 342685

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.

Upvotes: 18

Ionuț G. Stan
Ionuț G. Stan

Reputation: 179169

Why not replacing IDs with a class like .controls? Then just use:

$(".controls").hide();

Upvotes: 3

Canavar
Canavar

Reputation: 48088

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

$('.controlClass').hide();

Upvotes: 5

Related Questions