Solved

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

Posted on 2013-05-15
9
263 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 6

Expert Comment

by:nickinthooz
ID: 39169348
It didn't, I left that out on accident.
0
 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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)

809 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