0

I am using jquery .attr in an input type so when I run this

console.log('name: '+$(this).attr('name'));

output is: name: user_project_category[65][skill][9]

How can I get the 65 and 9?

5
  • 1
    Show your jquery code. Commented Feb 10, 2017 at 8:57
  • Hi this is my code in jquery Commented Feb 10, 2017 at 8:58
  • $('.tagTeam').each(function(index){ console.log('member id:' + $(this).val()); console.log('name: '+$(this).attr('name')); }); Commented Feb 10, 2017 at 8:58
  • and this is my input type attribute '<input class="form-control tagTeam" type="text" name="user_project_category['+category_id+'][skill]['+suggestion.skill_id+']" placeholder="Enter Members"/>';' Commented Feb 10, 2017 at 9:00
  • you are getting the client id Commented Feb 10, 2017 at 9:03

4 Answers 4

2

You can use Regular Expressions to extract text from between the brackets into an array and then you can access the array to get the values you want, you can either extract all text between brackets or just the numbers:

var yourInput = "user_project_category[65][skill][9]";
var allMatches = yourInput.match(/\[(.*?)\]/g);
console.log(allMatches[0]);
console.log(allMatches[2]);

var numberMatches = yourInput.match(/\[([0-9]*?)\]/g);
console.log(numberMatches[0]);
console.log(numberMatches[1]);

Sign up to request clarification or add additional context in comments.

Comments

0

var data = "name: user_project_category[65][skill][9]";


console.log(data.split("[")[1].slice(0,-1))//use split to get 65] use slice to remove ]
console.log(data.split("[")[3].slice(0,-1))//use split to get 9] use slice to remove ]

  1. You can use split with slice

Assuming this is not dynamic and format is the same. for dynamic use regex

1 Comment

Hi! this is dynamic but still thank ypu for your answer
0

Use regex.

var output = 'user_project_category[65][skill][9]';
var numbers = output.match(/\d+/g).map(Number);
alert(numbers);

output: 65,9

Do whatever you want to do with number.

working fiddle

1 Comment

Thanks everyone for your comment really helpful!
0

Use regular expression or split function in JavaScript

var output= 'user_project_category[65][skill][9]';
output.split(/(\d+)/)[1];
output.split(/(\d+)/)[3];

3 Comments

this is not an answer to the OP this is just a suggestion better put into comment
Below 50 reputation cannot comment.
sorry for my format this what Im trying for split var dataaa = $(this).attr('name'); var info = dataaa.split("[ ]"); alert(info);

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.