Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery: get the object name?

I am trying to retrieve the clicked object name through the code below, but I get [object object] only, how can I get the object name?

<a href="#" class="clickme">click</a>

    $('.clickme').click(function(){
      alert($(this));
      return false;
    });

I would like to get '.clickme' as my object name.

Thanks.

EDIT:

thanks for the help guys. sorry that for not being clear.

I want to get the object name dynamically when I turn this jquery click() into a customised function or a plugin.

so,

<a href="#" class="clickme">click</a>

        $('.clickme').click(function(){
          alert($(this));
          return false;
        });

I would like to get '.clickme' as my object name.

and,

<a href="#" id="clickme">click</a>

        $('#clickme').click(function(){
          alert($(this));
          return false;
        });

I would like to get '#clickme' as my object name.

so you notice sometime I want to get id name but sometime I want to get the class name as the object name.

like image 443
Run Avatar asked Apr 02 '11 16:04

Run


5 Answers

jQuery does have an internal property selector that can be used to find what you're looking for:

$('.clickme').selector == '.clickme';
$('.clickme').find('a').selector == '.clickme a';

However you can't access this within the click(function(e){}) since you have to reselect using this:

$('.clickme').click(function(e){
  alert($(this).selector); // alerts nothing
});

However, when you create the click function you can assign that element to a variable and reference it within:

var $cm = $('.clickme').click(function(e){
  alert($cm.selector); // alerts '.clickme'
});
like image 179
mVChr Avatar answered Oct 24 '22 03:10

mVChr


That's the class. Not the object name and certainly nothing unique.

You can get it using this.className or $(this).attr('class').

If you want something unique, use the id attribute and replace class with id.

like image 36
ThiefMaster Avatar answered Oct 24 '22 04:10

ThiefMaster


This doesn't responds to the exact question but to the one I got when I get there - So you too maybe ;-)

Short answer: use "this.name" which is the same than "$(this).attr('name')"

Sample:

$("*").click(function (event) {
   event.stopPropagation();
   // Display the name of the clicked object
   $(this).after("<span> " + this.name +" clicked! </span>");
   // Change the name for what you want (optional - just to show it's doable)
   this.name = this.name + "X";
});
like image 4
Le Droid Avatar answered Oct 24 '22 04:10

Le Droid


Name or id?

 $('.clickme').click(function(){
  alert( this.id );  // or $(this).attr('name') if you want the name
  return false;
});

What it really looks like is that you want the class (though, I'm not sure why since you've used the class as the selector).

 $('.clickme').click(function(){
  alert( $(this).attr('class') );
  return false;
});
like image 1
tvanfosson Avatar answered Oct 24 '22 04:10

tvanfosson


an alternative using the event object

$('.clickme').click(function(e){
  alert($(e.target).attr('class'));
  return false;
});
like image 1
Quincy Avatar answered Oct 24 '22 04:10

Quincy