2

suppose i have a .on() function wherein i select multiple ids

    $("#i, #am, #your, #father").on("click" , function(event){
    //iterate over the selectors and return tagname
       alert ( $("#i").prop("tagName")  );
       alert ( $("#am").prop("tagName") );
       alert ( $("#your").prop("tagName") );
       alert ( $("#father").prop("tagName") );
    }

the only method that i can think of is to alert each one separately. is there a known method for handing the $("#i, #am, #your, #father") as a sort of array?

user571099
  • 1,491
  • 6
  • 24
  • 42

2 Answers2

4

Try this please:

Interesting read: jQuery: What's the difference between '$(this)' and 'this'?

Also on click of any of these chained ids you can use $(this) to get prop tagName

Further if you are wondering how to bind all of them to on var see here: $foo example: http://jsfiddle.net/6hwLS/5/

Hope this helps,

$("#i, #am, #your, #father").on("click" , function(event){
//iterate over the selectors and return tagname
   alert($(this).prop("tagName")  );

}

further you can also do this

var $foo = $("#i, #am, #your, #father");

$foo.on("click", function(event) {
    //iterate over the selectors and return tagname
    alert($(this).prop("tagName"));

}​
Community
  • 1
  • 1
Tats_innit
  • 33,991
  • 10
  • 71
  • 77
  • the top rated comment says `Basically every time you get a set of elements back jQuery turns it into an array`. does this mean that by calling `.on()`, it not only sets events but also returns the elements in my selector? – user571099 Jun 30 '12 at 13:03
  • 1
    Hiya @user571099 cool, I reckon to understand `.on` plz read here :) http://api.jquery.com/on/ – Tats_innit Jun 30 '12 at 13:07
  • @user571099 no probs at all, glad it helped! – Tats_innit Jun 30 '12 at 13:20
2

Something like this ?

var $groupSelection = $('#i, #am, #your, #father');

$groupSelection.on('click', function(e){
   $groupSelection.each(function(){
      alert( $(this).prop('tagName') );
   });
});

Demo at http://jsfiddle.net/ZHpFa/

Gabriele Petrioli
  • 191,379
  • 34
  • 261
  • 317