Chris Carton
Chris Carton

Reputation: 59

Javascript Jquery How the get the name of $(this) Object

I'm interested by retrieving the name / object key (I dont know how to call that) of a $(this) object.

By example, when you do a console.log of $(this) in a click event, firebug print :

Object[h1#Title.uppercase.ui-draggable]

I'm interested by getting this :

h1#Title.uppercase.ui-draggable

How can I do it ?


EDIT :

I find a way to recompose what I want with a little script, here it is :

var tag = $(this).prop('tagName').toLowerCase();
var id = $(this).prop('id');

if(id.length>0){
  var id = '#'+id;
}else{
  var id = '';
}

var classes = $(this).prop('class');

if(classes.length>0){

  var class_list = classes.split(' ');
  var class_chain = '';			
  for(i=0;i<class_list.length;i++){
    class_chain = class_chain+'.'+class_list[i];
  }

}else{
  var class_chain = '';
}

var complete_chain = tag+id+class_chain;

Upvotes: 2

Views: 2471

Answers (1)

RAUSHAN KUMAR
RAUSHAN KUMAR

Reputation: 6006

If you don't want the jquery object and simply want to print the element, use this

console.log($(this)[0]);

If you are interested to get the name attribute of your html element, use this

console.log($(this).attr('name'));

If you want to same output as you want, use this

console.log($(this).prop("tagName")+"#"+$(this).attr('id')+"."+$(this).attr('class'));

Upvotes: 2

Related Questions