DonCallisto
DonCallisto

Reputation: 29932

JQuery attribute selector: wildcard for inner char

I've searched all the way down on the internet but I wasn't able to find what I looking for.

Let's say I have some inputs like those

<input name="kpr-xx-yyyy-mm-dd" />
<input name="kpr-zz-yyyy-mm-dd" />
<input name="kpr-kk-yyyy-mm-dd" />

where xx, zz, kk are some numeric id(s).

I would like to select all inputs that have name as kpr-*-yyyy-mm-dd
As far as I know, there isn't a "wildcard" selector for inner chars of a string.

I've tried with

$("input[name*=kpr][name*="+yyyy+"][name*="+mm+"][name*="+dd+"]");

maybe I can reach what I'm searching for, but I would like to restrict possible errors and search only for string I need (so same order, that in my example isn't taken into account)

Any ideas?

PS.: I don't know pretty much of jQuery, maybe solution is very simple and I don't know but, what I'm sure is that the www hasn't much about ...

Upvotes: 0

Views: 75

Answers (1)

iCollect.it Ltd
iCollect.it Ltd

Reputation: 93601

Combine a starts with selector with an ends with selector (the ends with needs to be a single selector like $='2012-12-12' rather than 3 separate selectors, hence the concatenation:

$("input[name^='kpr-'][name$='" + yyyy + "-" + mm + "-" + dd + "']");

I added inner single-quotes as your selectors have special characters (e.g. the '-')

JSFiddle: http://jsfiddle.net/TrueBlueAussie/r1xayf1t/

Upvotes: 4

Related Questions