Solved

Could you please explain live function in jquery

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

Report: Liquid Web beats Amazon, Rackspace & More

A study by performance analyst firm Cloud Spectator finds that Liquid Web beats rivals Amazon, Rackspace and DigitalOcean when it comes to website and cloud application performance.

Question has a verified solution.

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

Suggested Solutions

Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a pageā€¦
How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
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)
Suggested Courses

738 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