Arun
Arun

Reputation: 546

How to get the this DOM Object from the $(this) jQuery Object?

I have a function to which a jQuery object is passed as an argument. See below code.

$('selector').click(function() {
  // $(this) is the jquery object for the DOM on which I clicked.
  test($(this));
});
function test(jqobj) {
  // Here from jqobj, I want to get back the native javascript object (I mean the this)
}

Upvotes: 1

Views: 150

Answers (6)

Hitendra Patel
Hitendra Patel

Reputation: 123

So we can covert each other

//From jQuery object to Dom:
var domobject = jqueryObject.get();
//From Dom to jQuery object
var jqueryObject = jQuery(domobject);

Example:

 //From Dom to jQuery object
    var overlayContent = document.createElement("div");

Upvotes: 2

Amy
Amy

Reputation: 4032

If you are not passing multiple element use like this:

function test(jqobj) {
    var obj;
    if(jqobj[0]!=null)
    {
        obj = jqobj[0];
    }
  }

Upvotes: 0

Atul Nar
Atul Nar

Reputation: 695

rather passing object as a parameter you can also send element id or class to function.Then you can use getElementById method inside function to refer the element.

Upvotes: 0

ozil
ozil

Reputation: 7117

var __this=this; //declare out side your function.
function test(){
__this; //use __this in your function.
}

Upvotes: 1

Arun P Johny
Arun P Johny

Reputation: 388316

if you are sure that there will only be one element then

function test(jqobj) {
  var el  = jqobj[0]
}

Upvotes: 0

michalh
michalh

Reputation: 2997

The jqobj param is an array of DOM elements so you can use

     jqobj[0] 

but test the size of the array if it is not empty like

     jqobj.length > 0

Upvotes: 3

Related Questions