Solved

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

Posted on 2013-05-15
9
257 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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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 …
I have answered several questions lately that were solved utilizing the power of jQuery's AJAX functions, so I thought I would write an article demonstrating the ease of use. Why should I use jQuery as opposed to regular JavaScript? Now I know…
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…

758 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

17 Experts available now in Live!

Get 1:1 Help Now