0

I have array of input field like this;

<input type="text" name="pname[283]" value="" class="input" />
<input type="text" name="pname[678]" value="" class="input" />
<input type="text" name="pname[876]" value="" class="input" />
<input type="text" name="pname[454]" value="" class="input" />

Now i want to get the array indexes of every field inside foreach loop something like this

$('.input').each(function(e) {
   console.log('get array indexes');  eg: 283, 678, 876, 454  
 });
2
  • Does this answer your question? Extract embedded number from string - JavaScript Regex Commented Aug 5, 2020 at 15:27
  • What have you tried so far to solve this on your own? Any attempts at all? .index() + .slice(), or a regular expression, or .split(), or ... Commented Aug 5, 2020 at 15:41

1 Answer 1

0

You can parse the name attribute manually.

$('.input').each(function() {
   const name = $(this).prop('name');
   console.log(name.slice(name.indexOf('[') + 1, -1)); 
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" name="pname[283]" value="" class="input" />
<input type="text" name="pname[678]" value="" class="input" />
<input type="text" name="pname[876]" value="" class="input" />
<input type="text" name="pname[454]" value="" class="input" />

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

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.