7

Say I have two jquery selections:

var txtA = $('#txtA');
var txtB = $('#txtB');

In order to attach the same event function, is this the neatest way, or am I missing an obvious piece of syntax or jquery wizardness?

$(jQuery.merge(txtA , txtB )).click(function () { alert('hello'); });

Thanks.

maxp
  • 24,209
  • 39
  • 123
  • 201
  • if you already have the two selector variables (perhaps because you needed to do separate stuff to them already) then there are better ways... – Alnitak Aug 01 '11 at 10:13
  • @Felix your link refers to passing one string and letting the jquery engine seperate the queries, I have two (or more) strongly typed objects. – maxp Aug 01 '11 at 10:16
  • @maxp: Well, it was not necessarily clear how you wanted / had to select the elements. – Felix Kling Aug 01 '11 at 10:26
  • @Felix it's 100% clear - it's there in the first three lines. – Alnitak Aug 01 '11 at 10:38
  • @Alnitak: The title is *Jquery Selector - Multiple Items*. Selecting the elements beforehand might just be the way the OP *thought* it works. And if the only action he is doing is adding the click event handler, then using the multiple selector is better. Anyway... it is not really something we have to discuss about. – Felix Kling Aug 01 '11 at 10:41

4 Answers4

9

.add() should do (provided that you have already populated txtA and txtB and want the reuse those selections.):

txtA.add(txtB).click(function () {
    alert('hello');
});

Given a jQuery object that represents a set of DOM elements, the .add() method constructs a new jQuery object from the union of those elements and the ones passed into the method. The argument to .add() can be pretty much anything that $() accepts, including a jQuery selector expression, references to DOM elements, or an HTML snippet.

jensgram
  • 31,109
  • 6
  • 81
  • 98
2
$('#txtA, #txtB').click(function () { alert('hello'); });

may work for you

Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
  • Hi @Pranay Rana, I'm afraid that won't work it'll try to select '#txtA' in the context of '#txtB' http://api.jquery.com/jQuery/ I believe '#txtA', '#txtB' need to be in one string '#txtA, #txtB'. Correct me if i'm wrong. – Alex KeySmith Aug 01 '11 at 10:15
  • 2
    @Alex Key - for your info you can check this : http://api.jquery.com/multiple-selector/ – Pranay Rana Aug 01 '11 at 10:19
  • Hi @Pranay Rana now I'm confused... did someone just edit your answer? A a second ago it read $('#txtA', '#txtB') i.e. 2 parameters not $('#txtA, #txtB') (1 parameter). Either that or I'm going mad :-) I've undone the -1. – Alex KeySmith Aug 01 '11 at 10:22
  • His answer is not correct. txtA and txtB are both individual collections. He has answered the question by making wrong assumptions. – maxp Aug 01 '11 at 10:26
  • @Alex Key You're right about the `$('#txtA', '#txtB')` thing. I saw it too (but am confused that no edit is indicated). – jensgram Aug 01 '11 at 10:27
  • I've taken downvote off I think I must be going mad. I was pretty sure I read you answer as having to 2 variables. It's 29 degree's in the office - I think the heat is getting to me! – Alex KeySmith Aug 01 '11 at 10:27
  • Thanks @jensgram, I was really confused :-) – Alex KeySmith Aug 01 '11 at 10:28
  • edits made within 5 minutes of each other don't get recorded - it's to allow some grace for stuff spotted just after posting. This answer is still wrong, though. – Alnitak Aug 01 '11 at 10:37
1

Here is what I have done:

$()
    .add(elementOne)
    .add(elementTwo)
    .add(elementThree)
    .click(function() {
        alert('hello');
    });
0

You can just do:

$("#txtA, #txtB").click(function () { alert('hello'); });

The same as a CSS selector, cool huh! :-)

Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
Alex KeySmith
  • 16,657
  • 11
  • 74
  • 152
  • no, i have two strongly typed collections '#txtA' and '#txtB' are both just examples. – maxp Aug 01 '11 at 10:19
  • No worries @maxp I assumed wrongly. You may want to update your question with the explicit need for the strongly typed collections, as there is a couple of us that have misread it. – Alex KeySmith Aug 01 '11 at 10:31