- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is the difference between Grep and Filter in jQuery?
The grep() method finds an element and the filter() method returns elements matching a specific criteria.
jQuery grep function
Example
The grep() function is used to find an element of an array. You can try to run the following code to learn how to work with grep(),
<html> <head> <title>jQuery grep() function</title> <style> div { color: blue; } p { color: red; margin: 0; } </style> <script src="https://code.jquery.com/jquery-1.10.2.js"></script> </head> <body> <div></div> <p></p> <script> var arr1 = [ 1, 7, 4, 8, 6, 1, 9, 5, 3, 7, 3, 8, 5, 8, 2 ]; $( "div" ).text( arr1.join( ", " ) ); arr1 = jQuery.grep(arr1, function( n, i ) { return ( n !== 5 && i > 6 ); }); $( "p" ).text( arr1.join( ", " ) ); </script> </body> </html>
jQuery filter function
The jQuery filter() method will return elements matching a specific criteria.
Example
You can try to run the following code to learn how to work with jQuery.filter() method,
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script> $(document).ready(function(){ $("p").filter(".myclass").css("background-color", "blue"); }); </script> </head> <body> <h1>Tutorialspoint</h1> <p class="myclass">Free Text Tutorials</p> <p>Free Video Tutorials</p> <p>Connecting Tutors</p> </body> </html>
Advertisements