0

I was trying to do my front-end with this Codepen Template but when i'm trying to click on the signup button, it's not changing to signup form for which the code is in the index.js file, i've included hte index.js file in the login.html.erb but when i'm running the rails server the transition isn't working. My Rails Project, Codepen Template Here is the javascript code in index.js

document.querySelector('.img__btn').addEventListener('click', function() {
document.querySelector('.cont').classList.toggle('s--signup');  });

2 Answers 2

2

Try changing the path of index.js Add this line in app.html for including index js file .

<%= javascript_include_tag "index" %>
Sign up to request clarification or add additional context in comments.

3 Comments

I've already added index.js in the asstes/javascripts folder which is the default folder for Ruby on Rails but it's still not working.
@AashishKumar did u check the view source of page ?
no i haven't but the above answer by Sebastian worked, i have to include the js code in DOMContentLoaded, it worked, but i don't know about DOMContentLoaded, can you please tell me what it is??
1

Wrap your current JS code in app/assets/javascripts/application.js within a DOMContentLoadListener event listener:

document.addEventListener('DOMContentLoaded', function() {
  document.querySelector('.img__btn').addEventListener('click', function() {
    document.querySelector('.cont').classList.toggle('s--signup');
  });
});

4 Comments

As you have two times the same piece of code, it can be done in the application.js or in the index.js file. You then might consider removing the repeated one.
Do RoR add the application.js code automatically or i have to add it manually with javascript_include_tag??
Also thanks @Sebastion, you again saved the day, it worked but can you please tell what is it exactly??
The js code in the application.js file is executed "automatically" by Rails, it's added in your application erb file and this also adds all what you set by using //=, the code in it appears at the bottom of the compiled file. And about the DOMContentLoaded you can see here that's fired when the HTML has been totally loaded, so, this guarantees you all the needed file for your piece of code will be available @AashishKumar.

Your Answer

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.