Say I have a function that accepts multiple jQuery objects:
workWithThreeObjs($('.content'),
$('h1'),
$('input[type=button]'));
I define my function like this:
function workWithThreeObjs(p, h1, buttons) {
...
}
Inside this function, I want to apply border to all of them like this
$(p, h1, buttons).css(...);
How do I do that? Right now it only applies to p
.
My function code does not control what is passed into it, so I cannot change the selectors used to create the arguments. I must operate only on the jQuery objects provided.
Assuming that you want to use variables
Use $.fn.add()
Create a new jQuery object with elements added to the set of matched elements.
var p = $('.content'),
h1 = $('h1'),
buttons = $('input[type=button]');
p.add(h1).add(buttons).css({})
DEMO
var p = $('.content'),
h1 = $('h1'),
buttons = $('input[type=button]');
function workAnyNumberofObjects() {
for(var i=0; i<arguments.length; i++){
arguments[i].css('border', '1px solid blue')
}
}
workAnyNumberofObjects(p,h1,buttons);
You should be able to use any number of selectors here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With