Interview Questions on JQuery
31. How to write “ in string that is enclosed with in double quotes?
Use \”
32. How to remove extra white space?
The $.trim() function removes all newlines
Ex :-
var str = " lots of spaces before and after ";
$( "#original" ).html( "Original String: '" + str + "'" );
$( "#trimmed" ).html( "$.trim()'ed: '" + $.trim(str) + "'" );
33. How to create an array in javascript?
Method1 :- var cars = ["Saab", "Volvo", "BMW"];
Method2 :- var cars = new Array("Saab", "Volvo", "BMW");
34. How to find the size of an array?
Syntax :- arrayname.legth;
Ex:-
var fruits = ["Banana", "Orange", "Apple", "Mango"];
var legth = fruits.length;
length contains 4
35. How to sort the elements in alphabetical order in ascending?
Syntax : - arrayname.sort();
Ex:-
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.sort();
// fruits contains : Apple,Banana,Mango,Orange
36. How to sort the elements in alphabetical order in descending?
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.sort();
fruits.reverse();
// fruits contains : Orange,Mango,Banana,Apple
37. How to disable right click in jQuery?
$(document).ready(function(){
$(document).on("contextmenu",function(e){
if(e.target.nodeName != "INPUT" && e.target.nodeName != "TEXTAREA")
e.preventDefault();
});
});
38. How to submit a form without mouse click on submit button?
Use : document.forms["formname"].submit();
39. How to redirect to another page?
Use :- window.location="url_name";
40. How to hide particular div?
Syntax : - $("element").hide();
Ex :- $(".target").hide();