jQuery API - Part 1
jQuery has many functions, both synchronous and asynchronous which reduce the task of the programmer and are
- Efficient
- Reliable
- Fast
- Scalable
For getting the immediate ancestor of an element use
$("#element_id").parent();
$("#element_id").parent().parent();
To get / set attributes of an HTML element, use
attr() method.
Example:
If you want the value of the color to change from blue to red from a <p> element, use
let ret = $("#div1").attr("color","red")
If you want to get the current background color of a button having id="submit", use
let ret = $("#submit").attr("background-color");
If you want to set the value of a text field through jQuery, use
let ret = $("input[type=text]").val("Gaurav")
If you want to get a value of a text field through jQuery, use
let ret = $("input[type=text]").val();
If you want to get the in[nerHTML of an HTML element, use
let ret = $("#div1").html()
If you want to set the innerHTML of an HTML element, use
let ret = $("#div1").html("<span>Small Text</span>")
Here, in all the above 6 cases "ret" will contain the jQuery object of that respective element(s) marked by the selector.
$.ajax({
method: 'GET', //or "POST'
data: form1.serialize(),
url: 'http://destination_url/',
async: true
}).success(function(req,res){
console.log(res);
}).error(function(err){
throw err;
});
Let us understand simple code step-by-step:
There are four properties for that AJAX call:
- method - Defines the fully capital name of the HTTP method for sending the request
- data - The data to be sent in either XML or JSON format
- url - The uRL with an optional port numbre which idenfies the port on which the program is running
- async - IF true, the AJAX request-respomnse will run in the background and either success or error callback functions will be called on completion of HTTP session
The reponse property is a object containg various information about the response given back to the client.
For example res.responsText will return the response as plain text.
res.responseHTML will return the processed HTML markup.
The find() function
For example:
If there are three tables in an HTML file and there are 4 rows i.e., 4 <tr> tags and 4 columns i.e., 4 <td> tags
Then the code
$("td.last").find("tr")
will be the parent element that is the fourth row <tr> tag.Again to support method chaining, it will return a jQuery object with the selector as the parameter tothis.
The "this" keyword
Even callback functions have access to the this variable. Actually, this is an object denoted by $(this)
$(this) also supports method chaining.
let jQueryobj = $("<div />");
This will create a blank div tag which is not yet in the DOM
$(document).append(jQueryobj);
Comments
Post a Comment