Note that there are some explanatory texts on larger screens.

plurals
  1. POChange onclick action with a Javascript function
    primarykey
    data
    text
    <p>I have a button:</p> <pre><code>&lt;button id="a" onclick="Foo()"&gt;Button A&lt;/button&gt; </code></pre> <p>When I click this button the first time, I want it to execute Foo (which it does correctly):</p> <pre class="lang-javascript prettyprint-override"><code>function Foo() { document.getElementById("a").onclick = Bar(); } </code></pre> <p>What I want to happen when I click the button the first time is to change the onclick function from <code>Foo()</code> to <code>Bar()</code>. Thus far, I've only been able to achieve an infinite loop or no change at all. <code>Bar()</code> would look something like this:</p> <pre class="lang-javascript prettyprint-override"><code>function Bar() { document.getElementById("a").onclick = Foo(); } </code></pre> <p>Thus, clicking this button is just alternating which function gets called. How can I get this to work? Alternatively, what's a better way to show/hide the full text of a post? It originally starts shorted, and I provide a button to "see the full text." But when I click that button I want users to be able to click the button again to have the long version of the text go away.</p> <p>Here's the full code, if it helps:</p> <pre class="lang-javascript prettyprint-override"><code>function ShowError(id) { document.getElementById(id).className = document.getElementById(id).className.replace(/\bheight_limited\b/, ''); document.getElementById(id+"Text").className = document.getElementById(id+"Text").className.replace(/\bheight_limited\b/, ''); document.getElementById(id+"Button").innerHTML = "HIDE FULL ERROR"; document.getElementById(id+"Button").onclick = HideError(id); } function HideError(id) { document.getElementById(id).className += " height_limited"; document.getElementById(id+"Text").className += " height_limited"; document.getElementById(id+"Button").innerHTML = "SHOW FULL ERROR"; document.getElementById(id+"Button").onclick = "ShowError(id)"; } </code></pre>
    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