Solved

Could you please explain live function in jquery

Posted on 2013-02-06
3
314 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

770 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