Solved

How can I change this JQuery to use "on"?

Posted on 2013-05-15
9
260 Views
Last Modified: 2013-05-15
I'm new to JQuery and I need to change this code because it's deprecated. I've been looking at examples here
http://api.jquery.com/on/

.live is deprecated in Jquery 1.9. That article says to instead use "on".  This is the code I have. Should I just replace .live with "on" like this?

$(document).on('pageinit', function ()  


$(document).live('pageinit', function () {
	
	
	$(window).bind('pageshow', function () {
		addBodyClass();
	});
	
	function getActiveSec(){
		return $('section.ui-page-active').attr('id');
	}
	function addBodyClass(){
		$('body').attr('class', getActiveSec());
	}
});

Open in new window

0
Comment
Question by:Camillia
  • 5
  • 4
9 Comments
 
LVL 6

Accepted Solution

by:
nickinthooz earned 500 total points
ID: 39168868
try using document.ready

$(document).ready(function(){

	$(window).bind('pageshow', function () {
		addBodyClass();
	});
	
	function getActiveSec(){
		return $('section.ui-page-active').attr('id');
	}
	function addBodyClass(){
		$('body').attr('class', getActiveSec());
	}
});

Open in new window


The difference between using the event handler is that if you are pulling the page in from another then you'll be better off using .on, which yes you just replace live with on.  If you're loading the page directly, .ready is probably better for you.

If you're deadset on using on, you would use it like:

$(document).on('pageinit',function(){
    addBodyClass();
	});
	
	function getActiveSec(){
		return $('section.ui-page-active').attr('id');
	}
	function addBodyClass(){
		$('body').attr('class', getActiveSec());
	}
 });

Open in new window

0
 
LVL 7

Author Comment

by:Camillia
ID: 39169079
so what's the difference between the 3 ( "live", 'on" and "ready" )? I'm making the change but dont understand the difference and what I'm doing..

Let me try the code
0
 
LVL 7

Author Comment

by:Camillia
ID: 39169104
and one more question...i see you removed
$(window).bind('pageshow', function () {

from the orig code. Why did that need to be removed?
0
 
LVL 6

Expert Comment

by:nickinthooz
ID: 39169348
It didn't, I left that out on accident.
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 7

Author Comment

by:Camillia
ID: 39169359
oh, so it should the same as it was except "live" is replaced with "on"? correct?

$(document).on('pageinit',function(){
 $(window).bind('pageshow', function () {
    addBodyClass();
      });
      
      function getActiveSec(){
            return $('section.ui-page-active').attr('id');
      }
      function addBodyClass(){
            $('body').attr('class', getActiveSec());
      }
 });
0
 
LVL 6

Expert Comment

by:nickinthooz
ID: 39169367
To answer your other question:

$(document).on('ready',initialize); will not work if the DOM is already ready when the file is executed.

$(document).ready() has a special handling for this: it ensures it is always called


$(document).ready(function(){
//your code goes here
});

Open in new window


$(document).on('pageinit',function(){
//your code
 });

Open in new window


Those are the two difference in the beginning and ending, in the middle you would put:

$(window).bind('pageshow', function () {
		addBodyClass();
	});
	
	function getActiveSec(){
		return $('section.ui-page-active').attr('id');
	}
	function addBodyClass(){
		$('body').attr('class', getActiveSec());
	}

Open in new window

0
 
LVL 7

Author Comment

by:Camillia
ID: 39169381
Thanks, I took a JQuery class and still learning. Can you confirm my post ID: 39169359


is that correct? (and let me read your latest post)
0
 
LVL 6

Expert Comment

by:nickinthooz
ID: 39169411
Yes, that post looks correct, your code would look like:
$(document).on('pageinit',function(){
          $(window).bind('pageshow', function () {
	                	addBodyClass();
                    	});
	
	 function getActiveSec(){
		return $('section.ui-page-active').attr('id');
	}
	function addBodyClass(){
		$('body').attr('class', getActiveSec());
	}
 });

Open in new window

0
 
LVL 7

Author Comment

by:Camillia
ID: 39169418
you're great. Thanks for helping me out.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

by Julian Matz As of jQuery (http://jquery.com/) 1.4 the .live() method (http://api.jquery.com/live/) supports custom events as well as some standard JavaScript events that it previously didn't support. Among these is the submit event. Unlike …
What is a Lightbox? A Lightbox is the effect you see when you click, for example, an image and the screen fades out and up pops the same image but in its full size dimensions. There are lots of Lightbox effects for jQuery. Problem is they are a…
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)
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…

932 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now