jQuery – get javascript object

jQuery when applied on a css selector, returns a wrapper object which contains 0 or more javascript objects. To get the javascript object from it, we can take its 0-indexed item.

var jsObject= $(".box")[0];

The value will be undefined if there is no object. In case there are multiple javascript objects, it returns first object. Similarly we can get other javascript objects by taking 1,2,… indexed items.

Example

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>

<div class="box"></div>
<pre id="msg"></pre>

<script>
var $jqObject = $(".box");
var jsObject = $jqObject[0];
if (jsObject) {
  $("#msg").text(jsObject.nodeName);
}
</script>
refresh
Share this article: share on Google+ share on facebook share on linkedin tweet this submit to reddit

Comments

Click here to write/view comments