jQuery interview questions and answers – Part7

61. What are the types of selectors in jQuery?
There are three types of selectors in jQuery:
• CSS Selector
• XPath Selector
• Custom Selector

62. How jQuery selectors are executed?
Your last selectors is always executed first. For example, in below jQuery code, jQuery will first find all the elements with class “.myCssClass” and after that it will reject all the other elements which are not in “p#elmID”.
$(“p#elmID .myCssClass”);

63. Which is fast document.getElementByID(‘txtName’) or $(‘#txtName’).?
Native JavaScipt is always fast. jQuery method to select txtName “$(‘#txtName’)” will internally makes a call to document.getElementByID(‘txtName’). As jQuery is written on top of JavaScript and it internally uses JavaScript only So JavaScript is always fast.

64. How do you select an item using css class or ID and get the value by use of jquery?
If an element of html like < div> , < p> or any tag have ID MyId and class used MyClass then we select the element by below jquery code
$(‘#MyId’) for ID and for classs $(‘.MyClass’)
and for value
var myValue = $(‘#MyId’).val();
// get the value in var Myvalue by id
Or for set the value in selected item
$(‘#MyId’).val(“print me”);
// set the value of a form input

65. Difference between $(this) and ‘this’ in jQuery?
this and $(this) refers to the same element. The only difference is the way they are used. ‘this’ is used in traditional sense, when ‘this’ is wrapped in $() then it becomes a jQuery object and you are able to use the power of jQuery.
In below example, this is an object but since it is not wrapped in $(), we can’t use jQuery method and use the native JavaScript to get the value of span element.

66. How do you check if an element is empty?
There are 2 ways to check if element is empty or not. We can check using “:empty” selector.
if ($(‘#element’).is(‘:empty’)){
//Element is empty
And the second way is using the “$.trim()” method.
if($.trim($(‘#element’).html())==”) {
//Element is empty

67- How do You disable or enable a form element?
There are two ways to disable or enable form elements.
Set the ‘disabled’ attribute to true or false:
// Disable #pcds
$(‘#pcds’).attr(‘disabled’, true);
// Enable #pcds
$(‘#pcds’).attr(‘disabled’, false);
Add or remove the ‘disabled’ attribute:
// Disable #pcds
$(“#pcds”).attr(‘disabled’, ‘disabled’);
// Enable #x

68. How do you check or uncheck a checkbox input or radio button?
There are two ways to check or uncheck a checkbox or radio button.
Set the ‘checked’ attribute to true or false.
// Check #pcds
$(‘#pcds’).attr(‘checked’, true);
// Uncheck #pcds
$(‘#pcds’).attr(‘checked’, false);
Add or remove the ‘checked’ attribute:
// Check #pcds
$(“#pcds”).attr(‘checked’, ‘checked’);
// Uncheck #pcds

69. How do you get the text value of a selected option?
Select elements typically have two values that you want to access. First there’s the value to be sent to the server, which is easy:
// => 1
The second is the text value of the select. For example, using the following select box:
<select id=”pcdsselect”>
<option value=”1″>Mr</option>
<option value=”2″>Mrs</option>
<option value=”3″>Ms</option>
<option value=”4″>Dr</option>
<option value=”5″>Prof</option>
If you wanted to get the string “Mr” if the first option was selected (instead of just “1”), you would do that in the following way:
$(“#mpcdsselect option:selected”).text();
// => “Mr”

70. How do you check if an element exists or not in jQuery?
Using jQuery length property, we can ensure whether element exists or not.
if ($(‘#element’).length > 0){
//Element exists

Sign-up for our email newsletter and get free job alerts, current affairs and GK updates regularly.
Subscribe Here (or)

Leave a Reply

Your email address will not be published. Required fields are marked *

+ one = 4

Popup Dialog Box Powered By : XYZScripts.com
  • RSS
  • Facebook
  • Google+
  • Twitter