0

In my forms i have some arrays being sent back like this

<input class="checkbox-service" name="services['electricity']" type="checkbox"> 

<input class="checkbox-service" name="services['water']" type="checkbox"> 

<input class="checkbox-service" name="services['gas']" type="checkbox">

Now i need to be able to access all of these values in my jquery so i can somehow inject them into an input hidden, how can i do this in a way that doesn't make me have to duplicate code?

1
  • 1
    What have you attempted? Commented Dec 29, 2016 at 21:42

1 Answer 1

2

Easiest way would be to give each input a unique id attribute - then you can grab them by id. But if you want to use the name attribute, you'll need to escape those special characters when used in selectors:

var first_checkbox = $("input[name='services\[\\'electricity\\'\]']");

Note that you need to escape the single quotes twice... You might not need them either: for instance with PHP, they are not required and you could use "services[electricity]" directly.

Hope this helps!

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

1 Comment

I ended up using ID's as you suggested, it worked well, thank you for your help :)

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.