1

Hey guys i have this inputs

<input name='text[en]' value='aaaaaa' />
<input name='text[fr]' value='bbbb' />

I can obtain values with each using $(this).val(), but how i can obtain en,es that are inside de name

6 Answers 6

2

This will do it

var lang = this.name.match(/\[(.*)\]/)[1];
Sign up to request clarification or add additional context in comments.

Comments

1
$('input').each(function () {
    alert($(this).attr('name').match(/text\[([a-z]{2})\]/)[1]);
})

Comments

0
this.name

Will get you the full string like 'text[en]'. Then you can use a regex or substring to find the two letter code you are looking for.

An example would be:

var code = this.name.substring(5,6);

EDIT: Updated to reflect Gedrox's simplification.

1 Comment

There is no need to ask jQuery for name. this.name will be simpler.
0

Try this

$("input").each(function(){
   var name = $(this).attr("name");
   name = name.substring(name.indexOf("[") + 1, name.indexOf("]") - name.indexOf("]") -1);
});

Comments

0

You could do:

      var name = $(this).attr('name');
      var name = name.slice(-3, -1);

Comments

0

Use this to match the name to the regex

var match = /(.*)\[([^\]]+)\]/.exec($(this).attr('name'));

match[0] will contain the part before the square brackets, match[1] will contain the shorthand language code.

Comments

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.