0

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

1 Answer 1

1

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];
Sign up to request clarification or add additional context in comments.

1 Comment

Usernames could theoretically contain "_", you probably want to use every index but the last one instead of only the first index

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.