I would like to pass arguments to a function in a list, without having to keep a certain order and be able to pass only one, or no argument at all and so on.
I think the code is mostly ok, but for some reason I get the message that my arguments are not defined, although the function passes the object with the arguments.
What am I missing?
function positioning(options) {
var defaults = {
position: "static",
background: "lightblue",
height: "100px"
};
var params = $.extend({}, defaults, options);
var div1 = $('#div1'),
div2 = $('#div2');
console.log(options);
console.log(params);
console.log("position: " + position);
div2.css('position', position);
div2.css('background', background);
div2.css('height', height);
}
$('button').click(function() {
positioning({
height: "200px"
});
});
#div1 {
width: 100%;
height: 100px;
background: lightgrey;
}
#div2 {
width: 100%;
height: 100px;
background: lightblue;
top: 0;
left: 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="div1"></div>
<div id="div2"></div>
<button type="button">trigger function</button>
console.log("position: " + params.position);, in your function scope there is no variable namedpositioninstead it is a property of theparamsobejct