Posts

Showing posts from 2013

jQuery popup (dialog) form validation on a normal button click

jQuery Validation plugin, is no  doubt the first jquery plugins that is used by hundreds of users. Easy to use, pretty handy  and great user experience. For the users, what can be more better than having to know right away if they're doing it correctly.  There are  other blog where you can learn how to use the plugin correctly like one in here . Well,  most of the site explains about the integration process with great tutorial, however this one  is just to help those who wants to  implement the validation with simple button (not submit). Simple validation example using input type button's click event. HTML: Field 1: JQUERY: $(function() { $('#btn').click(function() { $("#form1").valid(); //validate form 1 }); }); Validation example using jQuery UI dialog box. HTML: Name Email Password JQUERY: $(function() { $('#dialogbtn').click(function() { $( "#dialo

Jquery :contains and making it Case-Insensitive

Well, playing around with jQuery, I once came accross jquery contains(). Very handy selector and easy to use and I have been using this in most of my scripts. So what does :contains do.. jQuery( ":contains(text)" ); Jquery contains selects all elements that contains the specified text. As the doc say, it selects all the elements which contains the given text. the elements can appear directly within the selected element, in any of that element's descendants, or a combination thereof. Example jQuery Javascript PHP JQUERY try it in fiddle The above script, will change the color of "Javascript" and "JQUERY". however, if you notice, the color of "jQuery" is not changed, though it starts with `J`. thus, :contains is case sensitive. The text must have matching case to be selected. What is you need contains with Case-Insensitive. One way to do this is , extending the old :contains selector. // OVERWRITES old selecor

Disable right click with jquery

jQuery makes it easy to disable the right click with just few lines of codes. bind contextmenu to the document and return false. however, this will disable the right click on entire document. $(function(){ //document.ready function $(document).on("contextmenu",function(e){ alert('right click disabled'); return false; }); }); to disable right click in certain elements, you can give it a class/id and use respective selector. Example right click disable right click right click no right click try it in fiddle

jquery attr() vs prop() (difference)

according to the docs 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: 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. on

Adding Fields(block) Using jQuery clone()

jQuery clone() creates a copy of matched element. It comes in handy since it does not only creates a copy of matched elements but also, all of their descendant elements and text nodes. Hello World Result Hello World World The above code creates a clone of div with class as world and appends it to div with class hello. Using clone(), we can create "Add more" without using any external js files or plugins FULL CODE HTML Name: Price Add More + CSS .close{ cursor:pointer; } JQUERY Try It In FIDDLE Yourself

Jquery 'On' (Events,Direct ,Delegated)

jQuery makes it easy to respond to user interaction with a web page. Example, this code listen to click event on all element in a document and alert the text in it. HTML click 1 click 2 JQUERY There are other methods for event binding such as click(), keydown(), keyup(), blur() and so on, all of which make use of jQuery's .on() method. jQuery's .on() method attachs an event handler function for one or more events on selected elements. The example given above is Direct event. which basically means : HEY!!!! I want every div to listen up: when you get clicked , give me alert. Here, each of these div has been individually given click event which works as it should. However  if you notice in jquery's DOC, According to the jquery's documentation: Event handlers are bound only to the currently selected elements; they must exist on the page at the time your code makes the call to .on(). so, now what if some of the div is generated dynamically