Learning jQuery

Tips, techniques, and tutorials for the jQuery JavaScript library
  • Filter:
  • All
  • Beginner
  • Intermediate
  • Advanced
  • Tutorials
    • Begin with jQuery
    • Setting up jQuery UI
    • Start with jQuery Mobile
    • jQuery with ASP.NET
  • AngularJS
    • AngularJS Interview Questions
    • Free ebooks to learn AngularJS
    • Bootstrapping AngularJS
    • All AngularJS articles
  • Interview Questions
    • jQuery Interview Questions
    • AngularJS Interview Questions
  • Plugins
    • 200+ jQuery Plugin Collection
    • Popup window plugin
    • LightBox Plugins
    • Text Effect Plugins
    • Best jQuery Plugins-2013
    • Best jQuery Plugins-2012
    • All Plugins
  • jQuery Mobile
    • Learn jQuery Mobile
    • jQuery Mobile Plugins
  • JQuery
    • Mobile
    • Plugins
  • Javascript
  • Resources
    • Mootools
    • Tutorials
  • Gallery
jQuery: Get row and column index of a GridView Cell
Difference between $(‘div’) and $(‘
‘) in jQuery

Apr 02 2013

jQuery: Difference between eq() and get()

by vikas
In this post, find out what is the difference between jQuery  eq() and  get() method. Both the methods are used to find and select single element from set of elements and they both return single "element". And they both accept single int type parameter, which denotes index.

Related Post:

  • Difference between $(this) and 'this' in jQuery
  • empty() vs remove() vs detach() - jQuery
  • jQuery - bind() vs live() vs delegate()

For example, take a look at below HTML. There is a <ul> element with 5 <li> elements.

<ul>
    <li>list item 1</li>
    <li>list item 2</li>
    <li>list item 3</li>
    <li>list item 4</li>
    <li>list item 5</li>
</ul>

And to select 3rd <li> element, use either get() or eq() and pass 2 as index. Keep in mind that index is zero-based.

$('li').get(2);
$('li').eq(2);

In the above code, both will return the 3rd <li> element. But then what is the difference between 2.

eq() returns the element as a jQuery object. This method constructs a new jQuery object from one element within that set and returns it. That means that you can use jQuery functions on it.

get() return a DOM element. The method retrieve the DOM elements matched by the jQuery object. But as it is a DOM element and it is not a jQuery-wrapped object. So jQuery functions can't be used.

$(document).ready(function () {
  $('li').eq(2).css('background-color', 'red'); //Works
  $('li').get(1).css('background-color', 'red'); // Error. Object #<HTMLLIElement> has no method 'css' 
});

Feel free to contact me for any help related to jQuery, I will gladly help you.


Scripts Used on This Site

Popular Posts

  • Using jQuery's Data APIs
  • Merging jQuery Deferreds and .animate()
  • A jQuery UI Combobox Under the Hood
  • Quick Tip: Prevent Animation Queue Buildup
  • Working with Events, part 1
  • Improved Animated Scrolling Script for Same-Page Links

Navigation

  • Home
  • Categories
  • Archives
  • About
  • Contact

Popular Posts

  • Using jQuery's Data APIs
  • Merging jQuery Deferreds and .animate()
  • A jQuery UI Combobox Under the Hood
  • Quick Tip: Prevent Animation Queue Buildup
  • Working with Events, part 1
  • Improved Animated Scrolling Script for Same-Page Links

Recommended Book

  • Learning jQuery, Fourth Edition book
    Learning jQuery,
    Fourth Edition
    By Karl Swedberg &
    Jonathan Chaffer

Links

Take A Look At Our Achives!

What Is This?

Learning jQuery is a multi-author weblog providing jQuery tutorials, demos, and announcements. We have tutorials for all skill levels, and each entry is categorized by level of difficulty.

Pages

  • Home
  • Categories
  • Archives
  • About
  • Contact
  • Cookies and privacy policy

RSS

  • Entries (RSS)
  • Comments (RSS)
Creative Commons License

© Copyright 2006–2025 Learning jQuery and participating authors. Written content on this site is under a Creative Commons License. Code examples are under your choice of MIT or GPL license.

Development & Design by Landocs with WordPress

Responsive Menu
Add more content here...