1

I'm building a script to update the url based on the checkboxes that the user selects. I found a interesting script that handles the update thing but I need to construct an object like this:

Object {foo: "bar", spam: "eggs", tracker: "yes"}

I already get the checkboxes values and push them into an array, but I don't know how to build an object similar to the above

My array looks like this in the console:

["brand:Brand3", "brand:Brand5", "size:Size1"]

This is my jsfiddle (see the output in the browser's console)

Any help is greatly appreciated.

7
  • I'm voting to close this question as off-topic because SO is not a code-writing service. Commented Apr 23, 2015 at 22:17
  • 1
    @MarcoBonelli, why voting to close? I wanted some direction on how to create the object I needed. I did not find a good resource on how to do it. Commented Apr 23, 2015 at 22:25
  • You should try to solve the problem, and then ask for help, showing the code you managed to write; this kind of question is off-topic. Commented Apr 23, 2015 at 22:32
  • Well, I did show the jsfiddle with my code. Anyway. Commented Apr 23, 2015 at 22:50
  • 1
    Don't make us go somewhere else to find your code, put it in the questions it self. A jsfiddle is always a good idea on top of code. Or better still use StackOverflow's CodeSnippet feature (the [<>] button in the editor) which will give us the code and working example all in one spot. Commented Apr 23, 2015 at 22:57

1 Answer 1

2

One way:

$('input[type="checkbox"]').on('change', function () {
    var url = document.URL;
    var addition = {};
    $('input').each(function(){
      addition[$(this).prop('name')] = $(this).val();
    });
    console.log(JSON.stringify(addition));
})

Js Fiddle

You might need to change the selector, depending if you have more inputs on the page.

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.