henrijs
henrijs

Reputation: 1010

Get object name as string

Here is jQuery cloned object in console log. Marked text is what I would like to get as string

enter image description here

Is that actual object name or something FireBug/jQuery has generated?

Upvotes: 1

Views: 260

Answers (2)

Alex Wayne
Alex Wayne

Reputation: 187262

There is not a built in way to get that value (which is a CSS selector that would include that element). Firebug is simply creating a preview for you.

But it's not hard to put together yourself.

var toSelector = function(element) {

  // start with the tag name
  var result = element.tagName.toLowerCase();

  // append #theid, if the element has an id
  if (element.id) {
    result += '#' + element.id;
  }

  // append .theclass for each class the element has
  if (element.className) {
    var classes = element.className.split(' ')
    for (var i = 0; i < classes.length; i++) {
      result += "." + classes[i];
    }
  }

  return result;
}

var element = document.getElementById('foo');
alert(toSelector(element));

Working example: http://jsfiddle.net/uzrxJ/1/

Upvotes: 1

Dr.Molle
Dr.Molle

Reputation: 117354

It's the selector for the element, used in firebug to visualize the object(the real value of target will be a reference to the Node)

Upvotes: 0

Related Questions