2

JavaScript isn't working in ruby on rails. I have included my JavaScript in the assets file and in the application.js with //=require_tree ..

the code of the javascript i am trying to run: $( document ).ready(function() {

$('#nav > div').hover(
 function () {
 var $this = $(this);
 $this.find('img').stop().animate({
 'width' :'199px',
 'height' :'199px',
 'top' :'-25px',
 'left' :'-25px',
 'opacity' :'1.0'
 },500,'easeOutBack',function(){
 $(this).parent().find('ul').fadeIn(700);
 });
 $this.find('a:first,h2').addClass('active');
 },
 function () {
 var $this = $(this);
 $this.find('ul').fadeOut(500);
 $this.find('img').stop().animate({
 'width' :'52px',
 'height' :'52px',
 'top' :'0px',
 'left' :'0px',
 'opacity' :'0.1'
 },5000,'easeOutBack');
 $this.find('a:first,h2').removeClass('active');
 }
);

});

asked Aug 9, 2015 at 9:15
3
  • 4
    It would be helpful if you provide detailed information about your case. Including the code example would be nice as well. Commented Aug 9, 2015 at 9:21
  • in the application layout i have used javascript_include_tag and added the application.js in which it requires the tree....but it is not loading. Commented Aug 10, 2015 at 10:13
  • <!DOCTYPE html> <html> <head> <meta http-equiv="content-type" content="text/html" charset="UTF-8"> <meta charset="utf-8" /> <title>WebApp</title> <%= stylesheet_link_tag 'application.css', media: 'all', 'data-turbolinks-track' => true %> <%= javascript_include_tag 'application', media:'all', 'data-turbolinks-track' => true %> <%= csrf_meta_tags %> </head> <body> Commented Aug 10, 2015 at 10:13

3 Answers 3

2

Make sure you are executing your javascript when the document is ready, like this (assuming you are using JQuery and Coffeescript):

$ -> # do you have this?
 alert 'test' 

Also check your browser's console for any errors.

UPDATE

Try to replace your code with this and see which alerts are showing...

$(document).ready(function() {
 alert(1)
 $('body').hover(
 function () {
 alert(2)
 }
 )
 $('#nav > div').hover(
 function () {
 alert(3)
 }
 )
})
answered Aug 9, 2015 at 9:44
Sign up to request clarification or add additional context in comments.

4 Comments

I did this...and i got the alert but the javascript isn't really doing anything
@suhabaobeid please, update your question with the javascript code you are trying to run
@devochka-volshebnita all alerts appeared...i am confused
@suhabaobeid so the problem is with your animation. try to remove this easeOutBack argument. if that doesn't work then try to apply your animation to $this instead of the img ($this.animate({ ....)
0

Based on what you've given. You can try to check this/these:

Make sure your included javascript files are before the ending:

//= require_tree .

An example is this:

//= require jquery
//= require jquery.turbolinks
//= require jquery_ujs
//= require turbolinks
//= require bootstrap-sprockets
//= require_tree .

Depending on your setup you might want to precompile those assets in which case it might be useful to edit config>initializers>assets.rb

An example should be included in the file, just make a new line and replace it with your javascript filename.

As always, see the documentation here as it might help figure out your issue:

http://guides.rubyonrails.org/asset_pipeline.html

answered Aug 9, 2015 at 9:26

1 Comment

yes it is like that...sorry there was a typo in the question..i edited it
0

When javascript isn't working I ask myself a couple questions.

Is jQuery loading correctly? Open up chrome console, can you type $ hit enter and not receive an error? If you receive something like the following code, jQuery is working.

// console test
$
=> jQuery(selector, context)

Then you've got javascript working. But most of the time, I run into an error because I'm not telling javascript to wait until the document is done loading because I've forgotten the following lines of code.

// regular jquery / javascript
$( document ).ready(function() {
 alert("This javascript thing is reals making me mad!");
});

Here's the same thing in Coffeescript

# Coffeescript
$(document).ready ->
 alert 'This javascript thing is reals making me mad!'
 return

If you receive alerts, then your javascript is working! However, the last question I might ask is, is my javascript file in the correct folder? Be sure to place your .js file withing the app/assets/javascripts/ folder. Then it will be loaded within the assetpipeline.

answered Aug 9, 2015 at 17:32

5 Comments

I actually did all the suggested and I did get the alert, I made the javascript to wait. I don't know what i am missing.....can i plz send u the code to check ? It is not much
Sure, you can send me the code and I'll check it. I'd say your JavaScript is working, but the JavaScript code you've written is not doing what you expect. You
how can i send it to u ? u prefer email or anything else? I can't post the whole code here...thnx
I'd upload it to github.com as a public repository. Then I can download it to my computer and test it out.
hey, you've still got to add the code into it. When I go there, it shows it as empty.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.