Using a string method

see the input in this fiddle here.

The name attribute value has number in it…1.

I want to get that number.I am assuming that this will be accomplished with a string method…but I do not know how to implement it exactly.
For instance how am I going to assign the value in that name attribute to a variable so I can than use the string method.

You have to extract the number from the name attribute using a RegExp (I think that would be the easiest).

My guess is that you might want to have a look at using regex to access it. Something like…

/[^0-9-.]/g

should remove all non-numeric characters. You can test it out on https://regex101.com/

yes…but how I am going to use it?

quick’n’dirty demonstrated for a single element:

var index = +document.querySelector('.service').name.match(/^form\[(\d+)\]/)[1]

can you PLEASE do it for this input element here

…the result must be 1.

$("input[name*='form']:first").attr('name').match(/^form\[(\d+)\]/)[1]

like that?

what is this [1] in the end of the line?

that gets the first submatch.

yes…your code works and I thank you for that.
In post3 though there is different regexp than yours…is it wrong?

it’s a different approach. and it relies on your target number to be the only number in the string. for me it’s too unspecific for this case (it also matches signed floats/integers, which are outside this question’s context).

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.