Using The jQuery .each() Function

<strong>jQuery each</strong>&nbsp;is one of the better-used functions in&nbsp;<strong>jQuery</strong>&nbsp;so I think it’s great to understand what you can do with it.

jQuery each is one of the better-used functions in jQuery so I think it’s great to understand what you can do with it.

The jQuery each function is used to loop over data the accessible way to think of it is that it’s similar to a foreach loop in other languages. So you can use it to loop over a number of DOM objects from the same selectors for a case if you want to add a target=”blank” to all links on the page then you will select all associations and loop through each of them to add a target=”blank”.

$('a').each(function(i){
    $(this).attr('target', '_blank');
});

Let us investigate how this works. Earliest in order we get all the anchor links on the page by using the following selector.

// Get all anchor links
$('a')

Next we adoption each function to loop through all the links.

$('a').each(function(i){
    // Performs tasks to each of the links
});

When you’re central the each function you can access the current element it is the loop through by using this keyword, but this article will not be a jQuery object and therefore if it’s a DOM aspect you will not be able to use any jury functions on it. The solution to this complication is to wrap this inside a jQuery object defined.

$('a').each(function(i){
    // Performs tasks to each of the links
    $(this);
});

When we have the current bashed element inside a jQuery object we can then add a new aspect to the link by using the attr function.

$('a').each(function(i){
    $(this).attr('target', '_blank');
});
GET THE CURRENT INDEX OF THE LOOP

In the above example, you’ll attend the I inside the function() parameters. This variable is colonized with the current index of the each to see this alive have 10 links on the page.

<a>Link 1</a>
<a>Link 2</a>
<a>Link 3</a>
<a>Link 4</a>
<a>Link 5</a>
<a>Link 6</a>
<a>Link 7</a>
<a>Link 8</a>
<a>Link 9</a>
<a>Link 10</a>

Then output the current index by watchful this to the user.

$('a').each(function(i){
    alert(i);
});
LOOP THROUGH ARRAYS

In the above example, you saw how you can select DOM elements and loop through them but you can also use it to curve through arrays of data and get both the indicator and the value of the area inside the array.

var fruit = ['orange', 'apple', 'banana', 'grapes', 'kiwi'];

$.each(fruit, function(index, value){
console.log(index + ' ' + value);
});

In the console, it will output the following.

“0 orange” “1 apple” “2 banana” “3 grapes” “4 kiwi”

LOOP THROUGH OBJECTS

What if you’re using objects to store data and not batch, each function will take care of that the same way as you can notice in the following code.

var fruitObj = {
1: 'orange',
2: 'apple',
3: 'banana',
4: 'grapes',
5: 'kiwi'
};

$.each(fruitObj, function(key, value){
console.log(key + ' ' + value);
});

The output of looping over the object is below.

“1 orange” “2 apple” “3 banana” “4 grapes” “5 kiwi”

See more:

jQuery Cheat Sheet – All in one

Javascript vs jQuery Examples


By : i'm programmer

jQuery Full Course - jQuery Tutorial From Beginner to Advanced - Learn jQuery 2019

jQuery is a cross-platform JavaScript library designed to simplify the client-side scripting of HTML. In this video, you'll learn jQuery from Beginner to Advanced

jQuery is open-source software using the permissive MIT License. Web analysis indicates that it is the most widely deployed JavaScript library by a large margin.

Mastering jQuery will boost up your career specially in web development. This course is designed you to master yourself in jQuery through step by step process.

Course Contents

  • jQuery Intro
  • What is jQuery and what you will learn?
  • Downloading jQuery
  • jQuery Basics
  • A first look at jQuery code
  • Selectors and Filters
  • Replacing contents
  • Handling events
  • Hide/Show events
  • Fading
  • Slide
  • Toggle
  • Animate
  • Selectors
  • Filters
  • jQuery Advanced
  • Advanced Selectors
  • Creating Content
  • Creating Content: Part 2
  • Inserting content
  • Modifying content
  • Modifying CSS
  • Final thoughts


What you’ll learn

  • Will become Master in client side web development
  • Will become expert in jQuery
  • Will earn working experience on jQuery
  • Are there any course requirements or prerequisites?
  • Prior experience on HTML and CSS required.
  • Who this course is for:
  • Who wanna expert in client side web development
  • Who wanna expert in jQuery


Thanks for watching

If you liked this post, share it with all of your programming buddies!

Follow us on Facebook | Twitter

How to use getContext() in jQuery

I'm trying to use&nbsp;getContext('2d');&nbsp;in jQuery. I have seen&nbsp;<a href="https://stackoverflow.com/questions/2925130/jquery-equivalent-of-getting-the-context-of-a-canvas" target="_blank">this question</a>, but it doesn't seem to work. It could possibly be from updates, since the question was asked near 9 years ago. I am adding&nbsp;&lt;script src='<a href="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js'&gt;&lt;/script&gt;" target="_blank">https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js'&gt;&lt;/script&gt;</a>&nbsp;in the head, but using a different&nbsp;&lt;script&gt;&nbsp;tag. Here is all my code:

I'm trying to use getContext('2d'); in jQuery. I have seen this question, but it doesn't seem to work. It could possibly be from updates, since the question was asked near 9 years ago. I am adding <script src='https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js'></script> in the head, but using a different <script> tag. Here is all my code:

var c = $('.canvas');
var ctx = $(c)[0].getContext('2d');

window.onload = function() {
$c.width(500);
$c.height(250);
$c.css({'background-color': '#e2e2e2', 'margin': '0 auto', 'display': 'block'});
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<canvas id='canvas'>You're browser does not support the requirements to play this game. Update your browser or use a different browser</canvas>


I'm using Google and checking the console through F12, and it says -

Uncaught TypeError: Cannot read property 'getContext' of undefined

Any help is appreciated! Thanks!

Difference between jQuery and AngularJS

Difference between jQuery and AngularJS

The clash between both of them is tougher. jQuery is a JavaScript library whereas AngularJS is a JS framework. JQuery has been into the existence since 2006 and is helping the developers in manipulating DOM with JS. Whereas, Angular was launched by Google in 2009 which focused on developing some powerful Single Page Apps. AngularJS and jQuery both play a vital role in the modern world of front-end development.

The clash between both of them is tougher. jQuery is a JavaScript library whereas AngularJS is a JS framework. JQuery has been into the existence since 2006 and is helping the developers in manipulating DOM with JS. Whereas, Angular was launched by Google in 2009 which focused on developing some powerful Single Page Apps. AngularJS and jQuery both play a vital role in the modern world of front-end development.

 

jQuery

It is necessary to understand jQuery as well as AngularJS individually. It is one of the most popular JS library. jQuery is a modular set of a cross-browser method for triggering the elements, choosing elements from the DOM, setting input values and much more. In simple terms, we can say that jQuery is a lightweight JS library that guides developers in clarifying the usage of client-side scripting for web apps using JS. Some key features and benefits of jQuery are

  • The content of a webpage can be manipulated easily
  • For creating gorgeous UI, you can use styles
  • AJAX calls can be made easily
  • HTML/DOM manipulation
  • Event Handling
  • Cross-browser compatibility
  • Lightweight

 

AngularJS

 

AngularJS is a robust JavaScript framework which was introduced by the tech giant Google in 2009. It brought revolutionary changes in the web development field, and the developer community warmly welcomed it. AngularJS app development has become a popular framework due to its fast-performing features. It also helps to create an interactive Single Page App (SPA) which offers terrific user experience.

 

Features of AngularJS:

 

•   Data Binding:

Data Binding provides following benefits to AngularJS Framework:

o   No need to write the code which synchronizes the view with the model and vice versa.

o   Simplified Programming model

o   A decrease in the number of Boilerplate coding.

 

•   Less Coding:

With the help of a conclusive User Interface, 2-way Data Binding & POJO Models, it enables you to form less amount of code in your project with greater capabilities.


•   Two-way Data Binding:

The 2-way Data Binding allows auto-synchronization between Model and View components. Data Binding enables you to stores the information and the data in such a way that it doesn't get repeated. Whereas in two-way Data Binding, if there are any changes made to any data, it will reflect the whole model.

 

•   MVVM Architecture:

AngularJS is furnished with MVVM(Model-View-View-Model) Architecture. The HTML template (of a component) is the view & the typescript part is the view model. Connecting the view to the view model is bindings.

 

•   Unit Testing:

Security is the biggest challenge faced by any company worldwide. The whole documentation is provided by the Unit Testing which will always be updated, including the instructions related to your motive of the code & its functional behaviour. The dominant benefit what Unit testing provide is that it forces you to use your code & encounter the defects in your design.

 

When to prefer jQuery ?

jQuery will be the first option if you are looking for a lightweight JS library along with DOM manipulation. jQuery also allows you to design and customize DOM along with JS. Even you can create a great website from a scratch if you adopt jQuery with vanilla JavaScript. However, you have to make sure that you use proper plugins for avoiding more bugs and issues. jQuery has some in-built features like event handling, AJAX support, and animation for making web development easier. You have to be very careful while adding the functionalities of jQuery plugin in Angular as the data binding within the apps can break.

 

When to prefer Angular?

Angular is the best option to go with when you are working on creating a web app. Angular processes on HTML code and JSON data which is helpful in creating robust and immersive apps. Angular is helpful in maintaining your web projects easily. The designing and customization in an Angular application can be performed quickly and stunningly with its two-way Data Binding and dependency injection.