Note that there are some explanatory texts on larger screens.

plurals
  1. POJQuery onclick firing constantly before the element is clicked
    primarykey
    data
    text
    <p>I've looked at more than a dozen questions about using <code>onclick</code>, <code>click</code>, <code>bind("click", ...)</code>, <code>on("click", ...)</code>, etc. and have yet to find the issue that I'm having.</p> <p>Basically it's an expandable div that hides some content when not expanded. I'm using the Twitter Bootstrap <code>collapse</code> class with a data-toggle button to expand/collapse the content itself, but I also need to modify the CSS of the container div to increase the height so that visually, the box the content is in will stretch to contain it.</p> <p>Here's my script code:</p> <pre><code>$(document).ready(function() { $("#expand-button").bind('click', expandClick($)); document.getElementById("#expand-button").bind("click", expandClick($)); }); function expandClick($) { $("#outer-container").animate({ "height": "350" }, 500); $("#expand-button").html("^"); $("#expand-button").bind("click", collapseClick($)); }; function collapseClick($) { $("#outer-container").animate({ "height": "50" }, 500); $("#expand-button").html("V"); $("#expand-button").bind("click", expandClick($)); } </code></pre> <p>The idea is simply that the handler rotates in and out depending on the state of the button. What's actually happening is that as soon as I load the page, the the expandClick function is immediately executed, which sets off an infinite loop of my container bouncing up and down despite nothing being clicked.</p> <p>Any ideas?</p> <p>Also, I don't think it should be relevant, but the HTML looks like:</p> <pre><code> &lt;div id="outer-container" class="container-fluid subsession-collapsed"&gt; &lt;div class="row-fluid" style="height: 50px"&gt; &lt;!-- OTHER STUFF... --&gt; &lt;div class="span1" id="4"&gt; &lt;button id="expand-button" class="btn-success" data-toggle="collapse" data-target="#expandable"&gt;V&lt;/button&gt; &lt;/div&gt; &lt;/div&gt; &lt;br /&gt;&lt;br /&gt; &lt;div id="expandable" class="row-fluid collapse"&gt; &lt;div class="span12" style="padding: 0 20px"&gt; &lt;!-- CONTENT --&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; </code></pre> <hr> <p>Edit:</p> <p>One SO topic I've used to try and find a solution is <a href="https://stackoverflow.com/questions/249074/how-to-change-onclick-handler-dynamically">this one</a>, but all of the responses have given the same result.</p>
    singulars
    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.
 

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