Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
171
rated 0 times [  172] [ 1]  / answers: 1 / hits: 57371  / 15 Years ago, wed, august 26, 2009, 12:00:00

I have a function called Colorbox (jQuery plugin) that takes a number of parameters like so:



$(this).colorbox({
width : 500px,
height : 500px
});


I have several different types of this, though, each with their own properties. Like so:



var Type = {
video: {
width : 500px,
height : 500px
},
gallery: {
width : 1065px,
height : 600px
}
}


Beyond that, I have other behaviors, logic, and a 'default' group of settings (which get overwritten by more specific ones). What I'm trying to do is push all the appropriate settings, from multiple objects, into a single Object so I can just call:



$(this).colorbox(Settings);


How would I transfer an unknown group of properties and their values (for example width and height) from something like Type.video into Settings? The goal is to be able to call Settings.height and get back the value I pushed in.


More From » jquery

 Answers
12

Take a look at the JQuery extend method. It can merge two objects together and all their properties.



From JQuery's example page:



var settings = { validate: false, limit: 5, name: foo };
var options = { validate: true, name: bar };
jQuery.extend(settings, options);


Now settings contains the merged settings and options objects.


[#98832] Thursday, August 20, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
victorr

Total Points: 193
Total Questions: 86
Total Answers: 105

Location: Pitcairn Islands
Member since Thu, Jun 24, 2021
3 Years ago
victorr questions
Fri, Nov 13, 20, 00:00, 4 Years ago
Sat, Jul 25, 20, 00:00, 4 Years ago
Thu, Jun 11, 20, 00:00, 4 Years ago
;