How To Do Simple Wildcard Selection In jQuery

Published Date Author: , Posted September 24th, 2010 at 12:42:59pm

Click here for the official jQuery Selectors documentation…

jQuery("input:radio[name^=foo]")

[code]
<div id="test_1">Test One</div>
<div id="test_2">Test Two</div>
<div id="test_3">Test Three</div>
[/code]
Show all divs with an ID starting with “test”:
[code]$("div[id^=test]").show();[/code]

Hide any element that has an ID starting with “test”:
[code]$("[id^=test]").hide();[/code]

Another interesting approach I found was to use a filter. In the following example only the first three lines would match:
[code]
<div id="abcd"></div>
<div id="abccd"></div>
<div id="abcccd"></div>
<div id="abd"></div>

$(‘div’)
.filter(function() {
return this.id.match(/abc+d/);
})
.html("Matched!")
;
[/code]

Lastly, a REGEX plugin…http://james.padolsey.com/javascript/regex-selector-for-jquery/

No comments as yet.

Leave Your Comment  Leave a comment

All fields marked with "*" are required.