Search code examples
jqueryfunctionextend

using jquery, how can I extend and pass an object to a function right at function call?


How can I do this in one line?

var myOptions = defaultOptions;
$.extend(myOptions, {width: 200});
$("#myField").foo(myOptions);

I dont want to make any changes to defaultOptions. I do not need myOptions anywhere else.


Solution

  • var newSettings = $.extend({}, defaultOptions, { width: 200 });
    $('#myfield').foo(newSettings);
    

    Or

    $('#myfield').foo($.extend({}, defaultOptions, { width: 200 }));