Avatar of depassion
depassionFlag for Philippines

asked on 

Jquery with rails 3

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

RubyWeb FrameworksJavaScript

Avatar of undefined
Last Comment
kristinalim
ASKER CERTIFIED SOLUTION
Avatar of kristinalim
kristinalim
Flag of Philippines image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of JESii
JESii
Flag of United States of America image

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.
Avatar of kristinalim
kristinalim
Flag of Philippines image

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.
JavaScript
JavaScript

JavaScript is a dynamic, object-based language commonly used for client-side scripting in web browsers. Recently, server side JavaScript frameworks have also emerged. JavaScript runs on nearly every operating system and in almost every mainstream web browser.

127K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo