ruby on rails - Javascript with ROR to toggle between div content -
i'm trying create feature users can toggle between 2 div content: "followers" or "pending" both buttons visible time.
javascript not working when click "pending" or "followers" button.
assets/javascript/followers.js
$(document).ready(function(){ var buttons = $("#buttons").find("a"); $("buttons").click(function() { $(".load-pending").click(function() { $("#followers").css("display", "none"); $("#pending").css("display", "block"); }); $(".load-followers").click(function() { $("#followers").css("display", "block"); $("#pending").css("display", "none"); }); }); }); followers.html.erb
<div class = "container"> <div id="buttons"> <a href="#" class="load-followers">followers</a> <a href="#" class="load-pending">pending</a> </div> <div id= "followers"> <% @followers.each |follower| %> <%= render 'user', user: follower %> <% end %> </div> <div id="pending" style= "display:none"> <% @requested_friendships.each |request| %> <%= render 'user', user: request %> <% end %> </div> </div>
you not need this...
$("buttons").click(function() { }); when this, assigning click event both buttons. click event assign new click events each button. (the ones have written class selectors).
you want remove call altogether , leave others directly under $(document).ready
here working js fiddle...
https://jsfiddle.net/bhb5xqqq/1/
- i substituted .css() calls .show() , .hide() - not necessary looks nicer in opinion.
Comments
Post a Comment