1

I'm getting the form data correctly using serializeArray() function, but im not sure how to change this data the object, which is = default_values

Here's my JavaScript:

$(document).on( 'click', '.anyicon-reset-button', function() {
     console.log($('#anyicon-live-style-form').serializeArray());
     console.log(default_values);
});

I want to remove the data from the serialized array, which is enter image description here

And set the data, which is in default_values object:

enter image description here

Any ideas are welcome!

2
  • You would need to build that object manually as there's no built in method of doing it. That being said, if you're just going to supply this to a $.ajax call, then you can just use serialize() Commented Sep 13, 2017 at 9:59
  • Not sure i understand you. Can you explain it with some examples ? Commented Sep 13, 2017 at 10:01

1 Answer 1

1

You can use .split() and $.each jQuery.each().

You can try placing the following code in your click event handler:

var inputs = $('#anyicon-live-style-form [name]');

// it would change the inputs in the form    
$.each(inputs, function(i, input){
    var split = $(input).attr('name').split('_')[1];
    $(input).val(default_values[split]);
})

//serializing again would give you reset data
$('#anyicon-live-style-form').serializeArray()


// if you only want to update the data of serializeArray 
// and don't show the change in the input, you can just run the following code

var ser = $('#anyicon-live-style-form').serializeArray();
$.each(ser, function(i, s){
    var split = s.name.split('_')[1];
    s.value = default_values[split];
});
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.