JavaScript How do I change serializeObject output format?

I'm quite new to javascript and can't find the solution. I have the following code.

<form class="edit-task-form">
        <legend>Create Task</legend>
        <label>Task</label>
        <input type="text" name="task" />
        <hr />
        <button type="submit" class="btn">Create</button>
    </form>

var taskDetails = $(ev.currentTarget).serializeObject();
var task = new Task;
task.save(taskDetails, {
    success: function(task) {
        alert(task.toJSON());
    }
});
console.log(taskDetails);

$.fn.serializeObject = function() {
var o = {};
var a = this.serializeArray();
$.each(a, function() {
    if(o[this.name] !==undefined) {
        if(!o[this.name].push) {
            o[this.name] = [o[this.name]];
        }
        o[this.name].push(this.value || '');
    }
    else {
        o[this.name] = this.value || '';
    }
});
return o;

The output is: Object {task: "fasdfasd"}

I would like it to be: Object {"task": "fasdfasd"}

How may I proceed to have "task" to appear in quotation marks?

Thanks a lot!

David

Answer:1

You have this function:

$.fn.serializeObject = function() {
var o = {};
var a = this.serializeArray();
$.each(a, function() {
    if(o[this.name] !==undefined) {
        if(!o[this.name].push) {
            o[this.name] = [o[this.name]];
        }
        o[this.name].push(this.value || '');
    }
    else {
        o[this.name] = this.value || '';
    }
});
return o;

you need to change it to this:

$.fn.serializeObject = function() {
var o = {};
var a = this.serializeArray();
$.each(a, function() {
    if(o['"'+this.name+'"'] !==undefined) {
        if(!o['"'+this.name+'"'].push) {
            o['"'+this.name+'"'] = [o[this.name]];
        }
        o['"'+this.name+'"'].push(this.value || '');
    }
    else {
        o['"'+this.name+'"'] = this.value || '';
    }
});
return o;

Just add to the key the quotes

Answer:2

Any help converting Excel "power of" function to JavaScript? Values are: A1 = 99% A2 = 45 formula is = (A1^A2)*100 results rounded to whole number is 64.

Any help converting Excel "power of" function to JavaScript? Values are: A1 = 99% A2 = 45 formula is = (A1^A2)*100 results rounded to whole number is 64.

  1. excel power of 2
  2. excel power of 10
  3. excel power of e
  4. excel power of 3
  5. excel power of symbol
  6. excel power of 2 formula
  7. excel power of function
  8. excel power of number
  9. excel power of two
  10. excel power of 4
  11. excel power of n
  12. excel power of negative number
  13. excel power of 2 symbol
  14. excel power of
  15. excel power of matrix
  16. excel power of calculation
  17. excel of power bi
  18. excel axis power of 10
  19. excel formula power of x
  20. excel formula power of n

I'm trying to scroll the page down when I click a button with an animation to a certain distance, currently, I'm able to scroll down to a certain element but this is not what I want, I want to scroll ...

I'm trying to scroll the page down when I click a button with an animation to a certain distance, currently, I'm able to scroll down to a certain element but this is not what I want, I want to scroll ...

  1. can't scroll down on certain websites

I have a dropdown selection field and other fields. It goes like this, <div class="form-group"><label class="sr-only" for="id_max_size">Max Size</label><select name="max_size"...

I have a dropdown selection field and other fields. It goes like this, <div class="form-group"><label class="sr-only" for="id_max_size">Max Size</label><select name="max_size"...

Does anyone know how to do a pull to refresh in a mobile site? I can see some conflicting issues that could be found from trying to implement this such as Chrome already does this from the browser by ...

Does anyone know how to do a pull to refresh in a mobile site? I can see some conflicting issues that could be found from trying to implement this such as Chrome already does this from the browser by ...

  1. angularjs pull to refresh