AJAX
AJAX
AJAX
AJAX Working
Ajax working Explanation:
1. An event occurs in a web page (the page is loaded, a button is clicked)
Example
HelHello, World! This text is loaded using Ajax.
2. Create the HTML for your application. In the following HTML, I've
used a basic HTML5 template containing an h1 element with an id attribute. I've
also created a button that will trigger the Ajax functionality on the page when
clicked.
3. <html>
4. <head>
5. <title>An Ajax Web Application</title>
6. </head>
7. <body>
8. <h1 id="page-title"></h1>
9. <button id="load-data">Click Here to Load the Data</button>
10. </body>
</html>
3.Create a script block in your HTML (just above the closing body tag)
and write an event listener to detect click events on the button.
<script>
document.getElementById("load-data").addEventListener("click",function(){
});
</script>});
</script>
xmlhttp.onreadystatechange=function() {
//Do something here
}}
6.Write a callback function to insert the data from the text file into
the h1 element when the state of the response changes.
xmlhttp.onreadystatechange = function() {
document.getElementById("page-title").innerHTML = this.responseText;
}
<html>
<head>
<title>An Ajax Web Application </title>
</head>
<body>
<h1 id="page-title"></h1>
<button id="load-data">Click Here to Load the Data </button>
<script>
document.getElementById("load-
data").addEventListener("click",function(){
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", "data.txt");
xmlhttp.onreadystatechange = function() {
document.getElementById("page-title").innerHTML =
this.responseText;
};
xmlhttp.send();
});
</script>
</body>
</html>
8.Open the HTML page in a browser and click the button. The script will produce the
following output in a browser:
Method Description
Property Description
Method Description
jQuery takes a lot of common tasks that require many lines of JavaScript
code to accomplish, and wraps them into methods that you can call with a
single line of code.
jQuery also simplifies a lot of the complicated things from JavaScript, like
AJAX calls and DOM manipulation.
jQuery - Plugins
A plug-in is piece of code written in a standard JavaScript file. These files provide
useful jQuery methods which can be used along with jQuery library methods.
There are plenty of jQuery plug-in available which you can download from
repository link at https://jquery.com/plugins.
Following example shows how to include jquery.plug-in.js plugin −
<html>
<head>
<title>The jQuery Example</title>
<body>
.............................
</body>
</html>
Following are the two different ways for adding the jQuery to Html page:
If we want to add the jQuery to Html page by downloading the jQuery file, then we
have to follow the steps which are given below. Using these steps, any user can
easily add the jQuery.
Step 1: Firstly, we have to download the jquery js file from the following
official site of jQuery. https://jquery.com/download/
<!Doctype Html>
<Html>
<Head>
<Title>
Add the jQuery file into Html by downloading and Including file
</Title>
</Head>
<Body>
Hello User!... <br> <center>
<img src="https://www.javatpoint.com/images/logo/jtp_logo.png" width="100" he
ight="100" > </center>
</Body>
</Html>
Step 3: After then, we have to place the cursor between the head tag
just before the title tag. And, then we have to use the <script> tag,
which specify the src attribute for adding the jQuery file.
<!Doctype Html>
<Html>
<Head>
<script type="text/javascript" src="jquery-3.5.1.min.js">
</script>
<Title>
Add the jQuery file into Html by downloading and Including file
</Title>
</Head>
<Body>
Hello User!... <br> <center>
<img src="https://www.javatpoint.com/images/logo/jtp_logo.png" width="100" he
ight="100" > </center>
</Body>
</Html>
Step 4: And, at last, save the Html file and the jQuery file is
successfully added into our Html page.
jQuery Syntax:
The jQuery syntax is tailor-made for selecting HTML elements and performing
some action on the element(s).
jQuery Selectors
jQuery selectors allow you to select and manipulate HTML element(s).
jQuery selectors are used to "find" (or select) HTML elements based on
their name, id, classes, types, attributes, values of attributes and much
more. It's based on the existing CSS Selectors, and in addition, it has
some own custom selectors.
All selectors in jQuery start with the dollar sign and parentheses: $().
SYNTAX: $("p")
Example:
$(document).ready(function(){
$("button").click(function(){
$("p").hide();
});
});
EXAMPLE PROGRAM:
<!DOCTYPE html>
<html>
<head>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js"></scrip
t>
<script>
$(document).ready(function(){
$("button").click(function(){
$("p").hide();
});
});
</script>
</head>
<body>
<h2>This is a heading</h2>
<p>This is a paragraph.</p>
</body>
</html>
The #id Selector
The jQuery #id selector uses the id attribute of an HTML tag to find the specific
element.
An id should be unique within a page, so you should use the #id selector when you
want to find a single, unique element.
To find an element with a specific id, write a hash character, followed by the id of
the HTML element:
Syntax: $("#test")
Example
When a user clicks on a button, the element with id="test" will be hidden:
$(document).ready(function(){
$("button").click(function(){
$("#test").hide();
});
});
To find elements with a specific class, write a period character, followed by the
name of the class:
Syntax: $(".test")
Example:
$(document).ready(function(){
$("button").click(function(){
$(".test").hide();
});
});
The JQUERY Document Ready Event
The ready event occurs when the DOM (document object model) has been loaded.
Because this event occurs after the document is ready, it is a good place to have all
other jQuery events and functions. Like in the example above.
The ready() method specifies what happens when a ready event occurs.
Syntax
Two syntaxes can be used:
$(document).ready(function)
The ready() method can only be used on the current document, so no selector is
required:
$(function)
Parameter Description
function Required. Specifies the function to run after the document is loaded
Example:
$(document).ready(function(){
$("p").click(function(){
$(this).hide();
});
JQuery Event Methods:
jQuery events are the actions that can be detected by your web application. They
are used to create dynamic web pages. An event shows the exact moment when
something happens.
o A mouse click
o An HTML form submission
o A web page loading
o A keystroke on the keyboard
o Scrolling of the web page etc.
click()
The click() method attaches an event handler function to an HTML element.
The function is executed when the user clicks on the HTML element.
The following example says: When a click event fires on a <p> element; hide the
current <p> element:
Example
$("p").click(function(){
$(this).hide();
});
EXAMPL E PROGRAM:
<!DOCTYPE html>
<html>
<head>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js"></scrip
t>
<script>
$(document).ready(function(){
$("p").click(function(){
$(this).hide();
});
});
</script>
</head>
<body>
<p>Click me too!</p>
</body>
</html>
dblclick()
The dblclick() method attaches an event handler function to an HTML element.
The function is executed when the user double-clicks on the HTML element:
Example
$("p").dblclick(function(){
$(this).hide();
});
mouseenter()
The mouseenter() method attaches an event handler function to an HTML element.
The function is executed when the mouse pointer enters the HTML element:
Example
$("#p1").mouseenter(function(){
alert("You entered p1!");
});
Example program:
<!DOCTYPE html>
<html>
<head>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#p1").mouseenter(function(){
alert("You entered p1!");
});
});
</script>
</head>
<body>
<p id="p1">Enter this paragraph.</p>
</body>
</html>
mouseleave()
The mouseleave() method attaches an event handler function to an HTML element.
The function is executed when the mouse pointer leaves the HTML element:
Example
$("#p1").mouseleave(function(){
alert("Bye! You now leave p1!");
});
mousedown()
The mousedown() method attaches an event handler function to an HTML
element.
The function is executed, when the left, middle or right mouse button is pressed
down, while the mouse is over the HTML element:
Example
$("#p1").mousedown(function(){
alert("Mouse down over p1!");
});
mouseup()
The mouseup() method attaches an event handler function to an HTML element.
The function is executed, when the left, middle or right mouse button is released,
while the mouse is over the HTML element:
Example
$("#p1").mouseup(function(){
alert("Mouse up over p1!");
});
hover()
The hover() method takes two functions and is a combination of
the mouseenter() and mouseleave() methods.
The first function is executed when the mouse enters the HTML element, and the
second function is executed when the mouse leaves the HTML element :
Example
$("#p1").hover(function(){
alert("You entered p1!");
},
function(){
alert("Bye! You now leave p1!");
});
<!DOCTYPE html>
<html>
<head>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#p1").hover(function(){
},
function(){
});
});
</script>
</head>
<body>
</body>
</html>
focus()
The focus() method attaches an event handler function to an HTML form field.
The function is executed when the form field gets focus:
Example
$("input").focus(function(){
$(this).css("background-color", "#cccccc");
});
<!DOCTYPE html>
<html>
<head>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("input").focus(function(){
$(this).css("background-color", "yellow");
});
$("input").blur(function(){
$(this).css("background-color", "green");
});
});
</script>
</head>
<body>
</body>
</html>
blur()
The blur() method attaches an event handler function to an HTML form field.
Example
$("input").blur(function(){
$(this).css("background-color", "#ffffff");
});
Example
$("p").on("click", function(){
$(this).hide();
});
<!DOCTYPE html>
<html>
<head>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("p").on({
mouseenter: function(){
$(this).css("background-color", "lightgray");
},
mouseleave: function(){
$(this).css("background-color", "lightblue");
},
click: function(){
$(this).css("background-color", "yellow");
});
});
</script>
</head>
<body>
</body>
</html>
jQuery Effects
Hide, Show, Toggle, Slide, Fade, and Animate are jquery methods.
With jQuery, you can hide and show HTML elements with
the hide() and show() methods:
Syntax:
1. $(selector).show();
2. $(selector).show(speed, callback);
3. $(selector).show(speed, easing, callback);
speed: It is an optional parameter. It specifies the speed of the delay. Its possible
vales are slow, fast and milliseconds.
Syntax:
$(selector).hide(speed,callback);
$(selector).show(speed,callback);
The optional speed parameter specifies the speed of the hiding/showing, and can
take the following values: "slow", "fast", or milliseconds.
$("#show").click(function(){
$("p").show();
});
Example program:
<!DOCTYPE html>
<html>
<head>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js"></scrip
t>
<script>
$(document).ready(function(){
$("#hide").click(function(){
$("p").hide();
});
$("#show").click(function(){
$("p").show();
});
});
</script>
</head>
<body>
<button id="hide">Hide</button>
<button id="show">Show</button>
</body>
</html>
jQuery toggle()
You can also toggle between hiding and showing an element with
the toggle() method.
Example
$("button").click(function(){
$("p").toggle();
});
Syntax:
$(selector).toggle(speed,callback);
The optional speed parameter can take the following values: "slow", "fast", or
milliseconds.
<!DOCTYPE html>
<html>
<head>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js"></scrip
t>
<script>
$(document).ready(function(){
$("button").click(function(){
$("p").toggle();
});
});
</script>
</head>
<body>
</body>
</html>
jQuery slideDown() Method
The jQuery slideDown() method is used to slide down an element.
Syntax:
$(selector).slideDown(speed,callback);
The optional speed parameter specifies the duration of the effect. It can take the
following values: "slow", "fast", or milliseconds.
Example
$("#flip").click(function(){
$("#panel").slideDown();
});
If the elements have been slid down, slideToggle() will slide them up.
If the elements have been slid up, slideToggle() will slide them down.
$(selector).slideToggle(speed,callback);
The optional speed parameter can take the following values: "slow", "fast",
milliseconds.
Syntax:
$(selector).fadeIn(speed,callback);
The optional speed parameter specifies the duration of the effect. It can take the
following values: "slow", "fast", or milliseconds.
Example
$("button").click(function(){
$("#div1").fadeIn();
$("#div2").fadeIn("slow");
$("#div3").fadeIn(3000);
});
Syntax:
$(selector).fadeOut(speed,callback);
The optional speed parameter specifies the duration of the effect. It can take the
following values: "slow", "fast", or milliseconds.
Example
$("button").click(function(){
$("#div1").fadeOut();
$("#div2").fadeOut("slow");
$("#div3").fadeOut(3000);
});
If the elements are faded out, fadeToggle() will fade them in.
If the elements are faded in, fadeToggle() will fade them out.
Syntax:
$(selector).fadeToggle(speed,callback);
The optional speed parameter specifies the duration of the effect. It can take the
following values: "slow", "fast", or milliseconds.
Syntax:
$(selector).fadeTo(speed,opacity,callback);
The required speed parameter specifies the duration of the effect. It can take the
following values: "slow", "fast", or milliseconds.
The required opacity parameter in the fadeTo() method specifies fading to a given
opacity (value between 0 and 1).
Example
$("button").click(function(){
$("#div1").fadeTo("slow", 0.15);
$("#div2").fadeTo("slow", 0.4);
$("#div3").fadeTo("slow", 0.7);
});
FUNCTIONS IN jQuery like text(),html(),val(),attr(),
css()
text() - Sets or returns the text content of selected elements
html() - Sets or returns the content of selected elements (including HTML
markup)
val() - Sets or returns the value of form fields.
Example:
$("#btn1").click(function(){
$("#test1").text("Hello world!");
});
$("#btn2").click(function(){
$("#test2").html("<b>Hello world!</b>");
});
$("#btn3").click(function(){
$("#test3").val("Dolly Duck");
});
The following example demonstrates text() and html() with a callback function:
Example
$("#btn1").click(function(){
$("#test1").text(function(i, origText){
return "Old text: " + origText + " New text: Hello world!
(index: " + i + ")";
});
});
$("#btn2").click(function(){
$("#test2").html(function(i, origText){
return "Old html: " + origText + " New html: Hello <b>world!</b>
(index: " + i + ")";
});
});
The following example demonstrates how to change (set) the value of the href
attribute in a link:
Example
$("button").click(function(){
$("#w3s").attr("href", "https://www.w3schools.com/jquery/");
});
The attr() method also allows you to set multiple attributes at the same time.
The following example demonstrates how to set both the href and title attributes at
the same time:
Example
$("button").click(function(){
$("#w3s").attr({
"href" : "https://www.w3schools.com/jquery/",
"title" : "W3Schools jQuery Tutorial"
});
});
A Callback Function for attr()
The jQuery method attr(), also comes with a callback function. The callback
function has two parameters: the index of the current element in the list of
elements selected and the original (old) attribute value. You then return the string
you wish to use as the new attribute value from the function.
Example
$("button").click(function(){
$("#w3s").attr("href", function(i, origValue){
return origValue + "/jquery/";
});
});
Syntax:
css("propertyname");
The following example will return the background-color value of the FIRST
matched element:
Example
$("p").css("background-color");
Set a CSS Property
To set a specified CSS property, use the following syntax:
css("propertyname","value");
The following example will set the background-color value for ALL matched
elements:
Example
$("p").css("background-color", "yellow");
AngularJS
Angular js is an open source Model-View-Controller frame work .
Features of ANGULAR JS
Data-binding − It is the automatic synchronization of data between model
and view components.
Model View Whatever − MVW is a design pattern for dividing an
application into different parts called Model, View, and Controller, each
with distinct responsibilities. AngularJS does not implement MVC in the
traditional sense, but rather something closer to MVVM (Model-View-
ViewModel). The Angular JS team refers it humorously as Model View
Whatever.
Scope − These are objects that refer to the model. They act as a glue between
controller and view.
Controller − These are JavaScript functions bound to a particular scope.
Services − AngularJS comes with several built-in services such as $http to
make a XMLHttpRequests. These are singleton objects which are
instantiated only once in app.
Filters − These select a subset of items from an array and returns a new
array.
AngularJS Directives
AngularJS lets you extend HTML with new attributes called Directives.
AngularJS directives are extended HTML attributes with the prefix ng-.
The ng-model directive binds the value of HTML controls (input, select,
textarea) to application data.