?
Solved

Could you please explain live function in jquery

Posted on 2013-02-06
3
Medium Priority
?
321 Views
Last Modified: 2013-02-11
Could you please explain live function in jquery and give me the understandable example
0
Comment
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 44

Accepted Solution

by:
Chris Stanyon earned 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
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…
Suggested Courses

650 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