6

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.

apsillers
  • 112,806
  • 17
  • 235
  • 239
Muhammad Umer
  • 17,263
  • 19
  • 97
  • 168

2 Answers2

10

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

Satpal
  • 132,252
  • 13
  • 159
  • 168
  • 1
    not sure why the discussion is continuing elsewhere, this is the answer. At least until someone finds the right duplicate to mark it with – CupawnTae Jun 03 '15 at 18:49
2
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

leapin_leprechaun
  • 605
  • 1
  • 4
  • 15
  • yes but i already have selectors deep in code, and they are specific based on some alogrithim. This example was meant to show what i wanted to do not how my code is. – Muhammad Umer Jun 03 '15 at 18:40
  • 1
    Then give a better example of what you're doing to show your problem. – David Thomas Jun 03 '15 at 18:41