Solved

Could you please explain live function in jquery

Posted on 2013-02-06
3
315 Views
Last Modified: 2013-02-11
Could you please explain live function in jquery and give me the understandable example
0
Comment
Question by:stmani2005
3 Comments
 
LVL 17

Expert Comment

by:sonawanekiran
ID: 38862846
First thing,
The live() method was deprecated in jQuery version 1.7. Use the on() method instead.

live method registers the method which is generated runtime.

For example

http://jsfiddle.net/gtEeU/
0
 
LVL 22

Expert Comment

by:Mrunal
ID: 38862883
0
 
LVL 43

Accepted Solution

by:
Chris Stanyon earned 500 total points
ID: 38863379
live() is now deprecated and on() is the preferred function. Basically it's a way of binding events to elements.

The simplest way to bind a click event to elements is to use something like:

('.someElements').click(function() { do something when clicked });

which is often run when the page is first loaded. The above example binds a click event to all elements with a class of 'someElements'.

Now imagine somewhere in your code you have a function that creates a new element with a class of someElements (an AJAX call perhaps). This new element wont have the click event bound to it, because the code that binds the event was run on page load, and your new element didn't exist then. This is where on() comes in handy. You can attach delegated events and the above line of code would be better off as something like:

$('body').on('click', '.someElements', function() { do something when clicked });

Now the click event will run on all elements with a class of someElements (that exist within body), even if they are added after the code is fired.

Read all about it here -> http://api.jquery.com/on/
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question