JQuery Interview Question Answer

1- What is jQuery ?

It's very simple but most valuable Question on jQuery means jQuery is a fast and concise JavaScript Library that simplifies HTML document traversing, animating, event handling, and Ajax interactions for rapid web development. jQuery is designed to change the way that you write JavaScript. Jquery is build library for javascript no need to write your own functions or script jquery all ready done for you

2- How you will use Jquery means requirement needed for using jquery?

Nothing more need to do just olny download jquery library(.js file) from any of the jquery site Download jquery and just linked with your html pages like all other javascript file

like below :

Code:

< script src="jquery.js" language="javascript" type="text/javascript">

3- what the use of $ symbol in Jquery?

$ Symbol is just replacement of jquery means at the place of $ you may use jquery hence $ symbol is used for indication that this line used for jquery

4- 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

Code:

$('#MyId') for ID and for classs $('.MyClass')

and for value

Code:

var myValue = $('#MyId').val();

// get the value in var Myvalue by id

Or for set the value in selected item

Code:

$('#MyId').val("print me");

// set the value of a form input

5- How to get the server response from an AJAX request using Jquery?

When invoking functions that have asynchronous behavior We must provide a callback function to capture the desired result. This is especially important with AJAX in the browser because when a remote request is made, it is indeterminate when the response will be received.

Below an example of making an AJAX call and alerting the response (or error):

Code:

$.ajax({
     url: 'pcdsEmpRecords.php',
     success: function(response) {
        alert(response);
     },
     error: function(xhr) {
        alert('Error!  Status = ' + xhr.status);
     }
});

6- How do you update ajax response with id " resilts"?

By using below code we can update div content where id 'results' with ajax response

Code:

function updateStatus() {
     $.ajax({
            url: 'pcdsEmpRecords.php',
            success: function(response) {
             // update div id Results
             $('#results').html(response);
         }
     });
}

7- 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:

Code:

// 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
$("#pcds").removeAttr('disabled');

8- 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.

Code:

// 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
$("#pcds").removeAttr('checked');

9- 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:

Code:

$("#pcdsselect").val();
// => 1

The second is the text value of the select. For example, using the following select box:

Code:

<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>
</select>

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:

Code:

$("#mpcdsselect option:selected").text();
// => "Mr"