Select and deselect all checkbox in table in jquery

We have a checkboz having id=selectall on selecting/deselecting we check/uncheck all check box in table whose clas=”checkbox”

$(function(){

// add multiple select / deselect functionality
$(“#selectall”).click(function () {
$(‘.checkbox’).attr(‘checked’, this.checked);
});

// if all checkbox are selected, check the selectall checkbox

$(“.checkbox”).click(function(){

if($(“.checkbox”).length == $(“.case:checked”).length) {
$(“#selectall”).attr(“checked”, “checked”);
} else {
$(“#selectall”).removeAttr(“checked”);
}

});
});

Sort a numeric array from small to largest in jquery

numArray.sort((a, b) => a – b); // For ascending sort numArray.sort((a, b) => b – a); // For descending sort

But above code will work only on ES6,6 supporting browser If you want to work in Old browser as well use below code.

 
<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Array Sort</h2>

<p>The sort() method sorts an Numeric array .</p>

<button onclick=”myFunction()”>Try it</button>

<p id=”demo”></p>

var array1 = [1, 30, 4, 21,0,12,21,11,456];
function myFunction() {
array1.sort(compareNumbers);
document.getElementById(“demo”).innerHTML = array1;
}
function compareNumbers(a, b)
{
return a – b;
}

</body>
</html>

 

What is method chaining in jquery

Method chaining in jquery is to apply multiple method in one go over an element. See below example..

var el=$(‘#mydiv’):

el.removeclass(‘oldclass’).addclass(‘class1’).CSS(‘color’:’red’);

In above example we removed a class then added another class and then applied a css. We didn’t applied seperately each method instead chained it.

What is contenttype and datatype in Ajax request

$.ajax({
  type: "GET",
  url: "/api/employees/getemployee",
  data: {'id:'+Id},
datatype : "json",
contentType: " application/json",


success
: function(data){

$("#resultarea”).text(data);

},
error: function (){
}

In above sample request we have both content type and datatype . Content type means type of data you sending to service/method. Datatype means type of data you are getting back from service/method.

In above sample we are sending json data and getting Jason in response.