Array and object provide $.each() method() for itration.We can iterate or loop over each element in an array or attribute of an object.
Example:
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>to iterating overarrays and objects with jQuery.each</title>
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script type="text/javascript">
$(document).ready(function () {
var months = ['office', 'excel', 'outlook', 'powerpoint', 'one note',
'one drive', 'puplisher'];
$.each(months, function (index, value) {
$('#microsoft').append('<li>' + value + '</li>');
});
var days = {
Sunday: 1, Monday: 2, Tuesday: 3, Wednesday: 4,
Thursday: 5, Friday: 6, Saturday: 7
};
$.each(days, function (key, value) {
$('#week').append('<li>' + key + ' (' + value + ')</li>');
});
});
</script>
<style type="text/css">
div {
display: inline-block;
float: left;
color: #fff;
font-size: 18px;
padding-left: 5px;
}
.first {
width: 150px;
height: 200px;
background: red;
}
.second {
width: 200px;
height: 200px;
background: lightgreen;
}
</style>
</head>
<body>
<div class="first" id="microsoft">microsoft products</div>
<div class="second" id="week">week days</div>
</body>
</html>
Output:
Post your comments / questions
Recent Article
- How to create custom 404 error page in Django?
- Requested setting INSTALLED_APPS, but settings are not configured. You must either define..
- ValueError:All arrays must be of the same length - Python
- Check hostname requires server hostname - SOLVED
- How to restrict access to the page Access only for logged user in Django
- Migration admin.0001_initial is applied before its dependency admin.0001_initial on database default
- Add or change a related_name argument to the definition for 'auth.User.groups' or 'DriverUser.groups'. -Django ERROR
- Addition of two numbers in django python
Related Article