Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery - Dynamically amend Input name with multiple dimensional array

I have an input field which looks something like this...

<input type='hidden' name='myInput[1][sausages][0][]' value='123' />

I need to change the value in the third set of square brackets when a user clicks on a button...

so something like

$("a.mylink").click( function() {
    $(this).siblings('input[name*=myInput]')..... CHANGE THE 0 to 1 in the third set of square brackets...
});

Any ideas how I can do this?

like image 871
Tom Avatar asked Jul 22 '26 14:07

Tom


1 Answers

Try This:

$('input').attr('name',$('input').attr('name').replace(/\[\d+](?!.*\[\d)/, '[1]'))

Working Demo

like image 112
Milind Anantwar Avatar answered Jul 24 '26 06:07

Milind Anantwar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!