Category Archives: Jquery

Post() Methods jQuery – AJAX get() and | Examples

  • The jQuery get() and post() methods are used to request data from the server with an HTTP GET or POST request.

HTTP Request: GET vs. POST :

  • Two commonly used methods for a request-response between a client and server are: GET and POST.
  • GET – Requests data from a specified resource
  • POST – Submits data to be processed to a specified resource
  • GET is basically used for just getting (retrieving) some data from the server. Note: The GET method may return cached data.
  • POST can also be used to get some data from the server. However, the POST method NEVER caches data, and is often used to send data along with the request.
  • To learn more about GET and POST, and the differences between the two methods, please read our HTTP Methods GET vs POSTchapter.

jQuery $.get() Method :

  • The $.get() method requests data from the server with an HTTP GET request.

Syntax:

$.get(<i>URL,callback</i>);
  • The required URL parameter specifies the URL you wish to request.
  • The optional callback parameter is the name of a function to be executed if the request succeeds.
  • The following example uses the $.get() method to retrieve data from a file on the server:

Example :

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$.get("demo_test.asp", function(data, status){
alert("Data: " + data + "\nStatus: " + status);
});
});
});
</script>
</head>
<body>

<button>Send an HTTP GET request to a page and get the result back</button>

</body>
</html>
  • The first parameter of $.get() is the URL we wish to request (“demo_test.asp”).
  • The second parameter is a callback function. The first callback parameter holds the content of the page requested, and the second callback parameter holds the status of the request.
  • Tip: Here is how the ASP file looks like (“demo_test.asp”):
<%
response.write("This is some text from an external ASP file.")
%>

jQuery $.post() Method :

  • The $.post() method requests data from the server using an HTTP POST request.

Syntax:

$.post(<i>URL,data,callback</i>);
  • The required URL parameter specifies the URL you wish to request.
  • The optional data parameter specifies some data to send along with the request.
  • The optional callback parameter is the name of a function to be executed if the request succeeds.
  • The following example uses the $.post() method to send some data along with the request:

Example :

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$.post("demo_test_post.asp",
{
name: "Donald Duck",
city: "Duckburg"
},
function(data,status){
alert("Data: " + data + "\nStatus: " + status);
});
});
});
</script>
</head>
<body>

<button>Send an HTTP POST request to a page and get the result back</button>

</body>
</html>
  • The first parameter of $.post() is the URL we wish to request (“demo_test_post.asp”).
  • Then we pass in some data to send along with the request (name and city).
  • The ASP script in “demo_test_post.asp” reads the parameters, processes them, and returns a result.
  • The third parameter is a callback function. The first callback parameter holds the content of the page requested, and the second callback parameter holds the status of the request.
  • Tip: Here is how the ASP file looks like (“demo_test_post.asp”):
<%
dim fname,city
fname=Request.Form("name")
city=Request.Form("city")
Response.Write("Dear " & fname & ". ")
Response.Write("Hope you live well in " & city & ".")
%>

jQuery AJAX Reference :

For a complete overview of all jQuery AJAX methods, please go to our jQuery AJAX Reference.

Example : 1

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$.post("demo_test_post.asp",
{
name: "Donald Duck",
city: "Duckburg"
},
function(data,status){
alert("Data: " + data + "\nStatus: " + status);
});
});
});
</script>
</head>
<body>

<button>Send an HTTP POST request to a page and get the result back</button>

</body>
</html>

Example : 2

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$.get("demo_test.asp", function(data, status){
alert("Data: " + data + "\nStatus: " + status);
});
});
});
</script>
</head>
<body>

<button>Send an HTTP GET request to a page and get the result back</button>

</body>
</html>

Load() Method in jQuery – AJAX |Example

jQuery load() Method :

  • The jQuery load() method is a simple, but powerful AJAX method.
  • The load() method loads data from a server and puts the returned data into the selected element.

Syntax: 

$(<i>selector</i>).load(<i>URL,data,callback</i>);
  • The required URL parameter specifies the URL you wish to load.
  • The optional data parameter specifies a set of querystring key/value pairs to send along with the request.
  • The optional callback parameter is the name of a function to be executed after the load() method is completed.

Here is the content of our example file: “demo_test.txt”:

<h2>jQuery and AJAX is FUN!!!</h2>
<p id="p1">This is some text in a paragraph.</p>
  • The following example loads the content of the file “demo_test.txt” into a specific <div> element:

Example :

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("#div1").load("demo_test.txt");
});
});
</script>
</head>
<body>

<div id="div1"><h2>Let jQuery AJAX Change This Text</h2></div>

<button>Get External Content</button>

</body>
</html>
  • It is also possible to add a jQuery selector to the URL parameter.
  • The following example loads the content of the element with id=”p1″, inside the file “demo_test.txt”, into a specific <div> element:

Example :

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("#div1").load("demo_test.txt #p1");
});
});
</script>
</head>
<body>

<div id="div1"><h2>Let jQuery AJAX Change This Text</h2></div>

<button>Get External Content</button>

</body>
</html>
  • The optional callback parameter specifies a callback function to run when the load() method is completed. The callback function can have different parameters:
  • responseTxt – contains the resulting content if the call succeeds
  • statusTxt – contains the status of the call
  • xhr – contains the XMLHttpRequest object
  • The following example displays an alert box after the load() method completes. If the load() method has succeeded, it displays “External content loaded successfully!”, and if it fails it displays an error message:

Example :

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("#div1").load("demo_test.txt", function(responseTxt, statusTxt, xhr){
if(statusTxt == "success")
alert("External content loaded successfully!");
if(statusTxt == "error")
alert("Error: " + xhr.status + ": " + xhr.statusText);
});
});
});
</script>
</head>
<body>

<div id="div1"><h2>Let jQuery AJAX Change This Text</h2></div>

<button>Get External Content</button>

</body>
</html>

jQuery AJAX Reference :

For a complete overview of all jQuery AJAX methods, please go to our jQuery AJAX Reference.

Example : 1

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("#div1").load("demo_test.txt");
});
});
</script>
</head>
<body>

<div id="div1"><h2>Let jQuery AJAX Change This Text</h2></div>

<button>Get External Content</button>

</body>
</html>

Example :  2

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("#div1").load("demo_test.txt #p1");
});
});
</script>
</head>
<body>

<div id="div1"><h2>Let jQuery AJAX Change This Text</h2></div>

<button>Get External Content</button>

</body>
</html>

jQuery – Get and Set CSS Classes |jQuery css() Method|jQuery Manipulating CSS|Example Stylesheet | jQuery addClass() Method

  • With jQuery, it is easy to manipulate the CSS of elements.

jQuery Manipulating CSS :

jQuery has several methods for CSS manipulation. We will look at the following methods:

  • addClass() – Adds one or more classes to the selected elements
  • removeClass() – Removes one or more classes from the selected elements
  • toggleClass() – Toggles between adding/removing classes from the selected elements
  • css() – Sets or returns the style attribute

Example Stylesheet :

  • The following stylesheet will be used for all the examples on this page:

 

.important </span>{
<span class="highATT">font-weight:</span><span class="highVAL"> bold;</span>
<span class="highATT">font-size:</span><span class="highVAL"> xx-large;</span>
}

<span class="highELE">.blue </span>{
<span class="highATT">color:</span><span class="highVAL"> blue;</span>
}

jQuery addClass() Method :

  • The following example shows how to add class attributes to different elements. Of course you can select multiple elements, when adding classes:

Example :

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("h1, h2, p").addClass("blue");
$("div").addClass("important");
});
});
</script>
<style>
.important {
font-weight: bold;
font-size: xx-large;
}

.blue {
color: blue;
}
</style>
</head>
<body>

<h1>Heading 1</h1>
<h2>Heading 2</h2>

<p>This is a paragraph.</p>
<p>This is another paragraph.</p>

<div>This is some important text!</div><br>

<button>Add classes to elements</button>

</body>
</html>
  • You can also specify multiple classes within the addClass() method:

Example :

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("#div1").addClass("important blue");
});
});
</script>
<style>
.important {
font-weight: bold;
font-size: xx-large;
}

.blue {
color: blue;
}
</style>
</head>
<body>

<div id="div1">This is some text.</div>
<div id="div2">This is some text.</div>
<br>

<button>Add classes to first div element</button>

</body>
</html>

jQuery removeClass() Method :

  • The following example shows how to remove a specific class attribute from different elements:

Example :

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("h1, h2, p").removeClass("blue");
});
});
</script>
<style>
.important {
font-weight: bold;
font-size: xx-large;
}

.blue {
color: blue;
}
</style>
</head>
<body>

<h1 class="blue">Heading 1</h1>
<h2 class="blue">Heading 2</h2>

<p class="blue">This is a paragraph.</p>
<p>This is another paragraph.</p>

<button>Remove class from elements</button>

</body>
</html>

jQuery toggleClass() Method :

  • The following example will show how to use the jQuery toggleClass() method. This method toggles between adding/removing classes from the selected elements:

Example :

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("h1, h2, p").toggleClass("blue");
});
});
</script>
<style>
.blue {
color: blue;
}
</style>
</head>
<body>

<h1>Heading 1</h1>
<h2>Heading 2</h2>

<p>This is a paragraph.</p>
<p>This is another paragraph.</p>

<button>Toggle class</button>

</body>
</html>

jQuery css() Method :

  • The jQuery css() method will be explained in the next chapter.

Test Yourself with Exercises! :

Example : 1

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
// add code here
});
</script>
<style>
.important {
font-weight: bold;
font-size: xx-large;
background-color: yellow;
}
</style>
</head>
<body>

<p>This is a paragraph.</p>

</body>
</html>

Example : 2

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
// add code here
});
</script>
<style>
.important {
font-weight: bold;
font-size: xx-large;
background-color: yellow;
}

.test {
color: red;
border: 5px dotted blue;
}
</style>
</head>
<body>

<p>This is a paragraph.</p>

</body>
</html>

Example : 3

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("p").click(function(){
// add code here
});
});
</script>
<style>
.important {
font-weight: bold;
font-size: xx-large;
background-color: yellow;
}
</style>
</head>
<body>

<p>This is a paragraph - Click me to toggle class!</p>

</body>
</html>

 

1 2 3 9