Solved

Jquery with rails 3

Posted on 2010-11-10
3
944 Views
Last Modified: 2012-05-10
this jquery code works when referenced internally but when i place it in the application.js file. it dosen't work
working when referenced like this:

##application.html.erb
<script type="text/javascript">  
	$(function () {
		setInterval("rotateImages()", 2000);

	});

	function rotateImages () {
		var oCurPhoto = $("#slideshow div.current");
		var oNxtPhoto = oCurPhoto.next();
		if (oNxtPhoto.length == 0)
			oNxtPhoto = $("#slideshow div:first");

		oCurPhoto.removeClass('current').addClass('previous');
		oNxtPhoto.css({ opacity: 0.0 }).addClass('current').animate({ opacity:1.0 }, 1000, 
			function () {
				oCurPhoto.removeClass('previous');
			});	
	}


but not like this:

##application.html.erb
	<%= javascript_include_tag "application" %>
  <%= javascript_include_tag "jquery-1.4.3" %>
	<%= javascript_include_tag "rails" %>
  <%= csrf_meta_tag %>

##application.js
(function () {
	setInterval("rotateImages()", 2000);

});

function rotateImages () {
	var oCurPhoto = $("#slideshow div.current");
	var oNxtPhoto = oCurPhoto.next();
	if (oNxtPhoto.length == 0)
		oNxtPhoto = $("#slideshow div:first");

	oCurPhoto.removeClass('current').addClass('previous');
	oNxtPhoto.css({ opacity: 0.0 }).addClass('current').animate({ opacity:1.0 }, 1000, 
		function () {
			oCurPhoto.removeClass('previous');
		});	
}

Open in new window

0
Comment
Question by:depassion
  • 2
3 Comments
 
LVL 4

Accepted Solution

by:
kristinalim earned 500 total points
ID: 34104211
Try moving your include tag for application.js after the include tag for the jQuery library. The problem with your situation seems to be that when your application.js is loaded, the dollar sign ($) is not yet defined, something that the jQuery library takes care of.
0
 
LVL 12

Expert Comment

by:JESii
ID: 34104375
I believe that's partially correct; but the standard Rails JS library (Prototype) also uses the $ tag, so I think you have to also tell jQuery to redefine the $ tag to something else, like this:
  jQuery.noConflict();
That will change the jQuery $ variable to jQuery

See http://api.jquery.com/jQuery.noConflict/ for more information.
0
 
LVL 4

Expert Comment

by:kristinalim
ID: 34104510
Definitely as JESii said, you would need jQuery.noConflict() if you also include the Prototype library.

By default the Prototype library is included in the Javascript expansion :defaults, so you need this if have something like:

<%= javascript_include_tag :defaults %>

or:

<%= javascript_include_tag "prototype" %>

in your layout.
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

770 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