Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2013-05-15
9
Medium Priority
?
300 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
[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
  • 5
  • 4
9 Comments
 
LVL 6

Accepted Solution

by:
nickinthooz earned 2000 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
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!

 
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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
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…

604 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