Note that there are some explanatory texts on larger screens.

plurals
  1. POUse Jquery .toggle in rails 3.1 app
    primarykey
    data
    text
    <p>I've been trying to get the .toggle function of Jquery to work in a rails 3.1 app such that a user can click a link to either show or hide content on the page they're viewing.</p> <p>The functionality I'm trying to replicate is the same as the example shown here: <a href="http://api.jquery.com/toggle/" rel="nofollow">http://api.jquery.com/toggle/</a></p> <hr> <pre><code>&lt;script&gt; $("button").click(function () { $("p").toggle(); }); </code></pre> <h2> </h2> <p>In my rails app, I have a users.js file found in app/assets/javascripts. The file only contains the following code:</p> <pre><code>$('#button').click(function(){ $('.cell').toggle(); }); </code></pre> <p>The view then has the following code:</p> <pre><code>&lt;table&gt; &lt;tr&gt; &lt;td class="cell" style="display: none"&gt; Some info &lt;/td&gt; &lt;td class="cell"&gt; Other info &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;input type="button" id="button" value="Show/hide" /&gt; </code></pre> <p>When I load the page, the button shows up along with the 'Other info', but when I click the button, no action it taken and nothing changes. When I inspect the source code of the page, it is definitely including my users.js file in the header.</p> <p>For some reason the javascript code won't execute the toggle and change the page content. I don't know if this is a Rails 3.1 issue, or not. Using a small standalone app, this functionality works fine when I include the script manually at the bottom of the page like:</p> <pre><code>&lt;script&gt; $("#button").click(function(){ $(".cell").toggle(); }); &lt;/script&gt; </code></pre> <p>Any thoughts on what I'm doing wrong with the jquery code or rails configuration? </p> <p>Thanks for your help!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload