Monday, 4 August 2014

Jquery FAQ


Explain important Jquery selectors? (Jquery Interview Questions)

June 5, 2014 at 12:02pm
You can select HTML elements using Jquery in the following ways:-

Select all

Below is a simple code snippet which selects all elements and hides them.


Select by Element

Below code selects all paragraph element tags in the HTML and hides them.


Select by ID

If your HTML elements have id’s like the below HTML code and you want to select the same by id.


Below is how the Jquery code by ID selection code would look like.


Select by class

If your HTML element has class names and you want to select them in jquery using them you need use the dot followed by the classname as shown in the below code.



Select by class and element

Consider the below HTML code where we have elements with “class” names. 


So if you wish to select by “element” and “class” in Jquery your selector code becomes something as below.
--------------

Jquery interview questions: - When do we need Document.Readyevent ?

HTML is loaded in to DOM by the browser in an interpreted manner. In other words every line is read from the HTML code and full finalDOM is created in memory after the last HTML statement is interpreted.

                              

Now take the example of the below Jquery code which is accessing “txt1”.  At the line where Jquery is calling the textbox , the textbox is not loaded in DOM so this line will crash.

<body>
<script language=javascript>
       $("#txt1").val("Hello Jquery");->Txt1 not loaded in DOM
</script>
<input type="text" id="txt1" />
</body>

This is where “document.ready”Jquery function comes to help and below is how we write it. The “ready” event fires when the complete DOM is loaded in memory. So now the below Jquery script will execute once all elements are loaded and thus will not have issues accessing the text box “txt1”.

<script language=javascript>
$(document).ready(
function ()
{
$("#txt1").val("Hello Jquery");
}
);
</script>

What is $.Ajax in jQuery?

It allows us to invoke server side functionalities asynchronously. It internally uses XmlHttpRequest.

What kind of requests we can make using $.ajax?

We can make any request like get, post, put and delete. $.ajax function accepts parameters called type which will decide what request we want do.

What is the difference between $.ajax and $.post?
  • $.post is simpler representation of $.ajax for POST requests. When we are using $.posits not required to specify the type. It will be always ‘post’.
  • If you are willing to make a post request and we expecting have more control over HTTP headers. $.ajax is the best choice. If your only invention is to make a post hit to server $.post is best.
What is the difference between contenttype and datatype in $.ajax?

ContentType represents the data type of data we are going to send from Javascrip whereas datatype represent the data type of data we are expecting from Serverside in return.

What is $.getJSON in jQuery?

It is just shorthand representation of $.ajax where type is set to “GET” and datatype is set to “JSON” automatically.








No comments:

Post a Comment