Note that there are some explanatory texts on larger screens.

plurals
  1. POJavaScript subtraction problem
    primarykey
    data
    text
    <p>I thought I'd write a simple script to animate my webpage a little.</p> <p>The basic idea was to make a div grow bigger and smaller, when I push a button. I handled the growing part well, but I can't get it to shrink again.</p> <p>It would seem, after long debugging, that the subtraction in JavaScript just isn't working. Basically, it seems, that this line isn't working: <code>i = height - 4;</code>, the value <code>i</code> stays the same.</p> <p>I'm including a complete example, could you help me fix it?</p> <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt; &lt;head&gt; &lt;title&gt;Test&lt;/title&gt; &lt;meta http-equiv="Content-Language" content="Estonian" /&gt; &lt;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-15" /&gt; &lt;/head&gt; &lt;body&gt; &lt;script type="text/javascript"&gt; var i; var increasing = new Boolean("true"); var height; function testFunction() { height = parseInt(document.getElementById("testDiv").offsetHeight); if( increasing == true ) { i = height + 4; document.getElementById("testDiv").style.height = i + "px"; if( height &gt;= 304 ) { increasing = false; } else { pause(30); } } else { i = height - 4; document.getElementById("testDiv").style.height = i + "px"; if( height &lt;= 104 ) { increasing = true; } else { pause(30); } } } function pause(ms) { setTimeout("testFunction()", ms); } &lt;/script&gt; &lt;button id="button" type="button" onclick="testFunction();"&gt;Do it!&lt;/button. &lt;div id="testDiv" style="border: 2px solid black; width: 400px; height: 100px; text-align: center;"&gt; Hello! &lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p><strike>EDIT: tags were producing HTML, had to change > to .</strike></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.
    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