22

A regular dom element is such as:

document.getElementById('a')

or

document.createElement('div')

But jQuery returns elements in another format, so for instance I'd like to convert what is returned by $('#a') to the same result as what is returned by document.getElementById('a')

Is this possible using jQuery?

thanks

Adrien Be
  • 18,545
  • 18
  • 99
  • 136
zjm1126
  • 58,281
  • 75
  • 169
  • 215

3 Answers3

44

You can reference the DOM element with .get(0) or [0], eg $('#foo')[0] assuming there is just one.

meder omuraliev
  • 177,923
  • 69
  • 381
  • 426
7
$('#a')[0]

// oops my answer is too short

Anantha Kumaran
  • 9,611
  • 8
  • 32
  • 36
7

That is what get() method was made for by JQuery team, consider:

var elem = $('#a')[0];

Or

var elem1 = $('#a').get(0);
var elem2 = document.getElementById('a');
alert(elem1 === elem2);

or longer version:

<!DOCTYPE HTML>
<html>
<body>   
<a id='a'></a>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js" type="text/javascript"></script>
  <script>

    var elem1 = $('#a').get(0);
    var elem2 = document.getElementById('a');
    alert(elem1 === elem2);

  </script>  
</body>
</html>
Dan
  • 52,315
  • 38
  • 111
  • 148
Sarfraz
  • 367,681
  • 72
  • 526
  • 573
  • 1
    This should really be the selected answer. When you used the === (relational operator), it really drove it home for me. Thanks! – Benji A. Nov 06 '18 at 15:04