Jan
Jan

Reputation: 16074

jQuery matching a selector with regex

I have a form and wanna reuse some code (DRY).

Here is a part of the form:

  <select id="user_country" name="user[country]"><option value="AT">Austria</option>

I wanna match the word user depending on the

ID

xxxxxx_country

or on the NAME

xxxxxx[country]

how can I get the xxxxxx ?

Many thanks

Upvotes: 0

Views: 53

Answers (1)

tckmn
tckmn

Reputation: 59273

If you already have the DOM element:

theElement.id.split('_')[0];

Or using regex:

theElement.id.match(/[^_]+/)[0];

If not, you can find it like this:

var theElement = $('select[id$="_country"]')[0];

Upvotes: 1

Related Questions