Wednesday, 4 May 2016

attr() vs prop() difference in Jquery

Direct Answer : 

attr() gives you the value of element as it was defines in the html on page load. It is always recommended to use prop() to get values of elements which is modified via javascript/jquery , as it gives you the original value of an element's current state. 

In Details : 

jQuery.attr()
Get the value of an attribute for the first element in the set of matched elements.

whereas,

 jQuery.prop()
Get the value of a property for the first element in the set of matched elements.

Before jQuery 1.6 , the attr() method sometimes took property values into account when retrieving some attributes, which caused in inconsistent behavior. And thus, the prop() method was introduced. As of jQuery 1.6. , the .prop() method provides a way to explicitly retrieve property values, while .attr() retrieves attributes. 

What actually is Attributes?
Attributes carry additional information about an HTML element and come in name=”value” pairs. You can set an attribute for HTML element and define it while writing the source code. 

simple example can be: 
?
1
<input id="test" type="test" value="test">
here, "type","value", "id" are attributes of the input elements. 

What is Property? 
Property is a representation of an attribute in the HTML DOM tree. once the browser parse your HTML code ,corresponding DOM node will be created which is an object thus having properties. 
in above case ,once browser renders the input, other properties like align, alt, autofocus, baseURI, checked so on will be added ...

since, attr() gives you the value of element as it was defines in the html on page load. It is always recommended to use prop() to get values of elements which is modified via javascript/jquery , as it gives you the original value of an element's current state. 

Tuesday, 27 May 2014

jQuery differed object

The Deferred object is chainable, similar to the way a jQuery object is chainable, but it has its own methods. After creating a Deferred object, you can use any of the methods below by either chaining directly from the object creation or saving the object in a variable and invoking one or more methods on that variable.

It can register multiple callbacks into callback queues, invoke callback queues, and relay the success or failure state of any synchronous or asynchronous function.

Monday, 12 May 2014

Get checked radio button value in jquery

To get the value of the selected radioName item of a form called 'myForm':

$('input[name=radioName]:checked', '#myForm').val()
To get the value of the selected radioName item using "class" or "id" 
$('.classname[name=radioName]:checked').val()

Saturday, 24 August 2013

What is jQuery?

 jQuery is fast, lightweight and feature-rich client side JavaScript Library/Framework which helps in to traverse HTML DOM, make animations, add Ajax interaction, manipulate the page content, change the style and provide cool UI effect. It is one of the most popular client side library and as per a survey it runs on every second website.

Why do we use jQuery?

Due to following advantages.
  • Easy to use and learn.
  • Easily expandable.
  • Cross-browser support (IE 6.0+, FF 1.5+, Safari 2.0+, Opera 9.0+)
  • Easy to use for DOM manipulation and traversal.
  • Large pool of built in methods.
  • AJAX Capabilities.
  • Methods for changing or applying CSS, creating animations.
  • Event detection and handling.
  • Tons of plug-ins for all kind of needs.

How JavaScript and jQuery are different?

 JavaScript is a language While jQuery is a library built in the JavaScript language that helps to use the JavaScript language.

Is jQuery replacement of Java Script?

 No. jQuery is not a replacement of JavaScript. jQuery is a different library which is written on top of JavaScript. jQuery is a lightweight JavaScript library that emphasizes interaction between JavaScript and HTML. 

Next Interview Question