Note that there are some explanatory texts on larger screens.

plurals
  1. POA More Elegant way to change the value of my div?
    primarykey
    data
    text
    <p>Ok so, what I need to do is change some numbers on the screen when keys are pressed, i have it working but I'm not happy with it. </p> <p>I have a input field and a div that is being updated when the value of the input filed changes. I did that so that I would not be constrained by the input field's blinking carat etc. I don't want to display the input field at all but when I hide it with CSS or type="hidden" it no longer works. I was trying to get this working with JS variables but so far have been unsuccessful. </p> <p>Any ideas? </p> <p>HTML</p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;link rel="stylesheet" type="text/css" href="number.css" /&gt; &lt;/head&gt; &lt;body onLoad="focus();myText.focus();changeNumber()"&gt; &lt;div id='number'&gt; &lt;/div&gt; &lt;input type="text" id="myText" value="1"/&gt; &lt;/body&gt; &lt;footer&gt; &lt;script type="text/javascript" src="number.js"&gt;&lt;/script&gt; &lt;/footer&gt; &lt;/html&gt; </code></pre> <p>JAVASCRIPT</p> <pre><code>var myText = document.getElementById("myText"); function changeNumber(){ var userInput = document.getElementById('myText').value; document.getElementById('number').innerHTML = userInput; } // Capture keyDown events myText.onkeydown = function(e) { // "34" is the up arrow key if (e.keyCode == 34) { // increment the value in the text input myText.value++; changeNumber() // "33" is the down arrow key } else if (e.keyCode == 33 &amp;&amp; myText.value &gt; 1) { // decrement the value in the text input myText.value--; changeNumber() } } </code></pre> <p>HERE IN THE FINAL FIXED CODE <strong>* THANKS GUYS! **</strong></p> <p>HTML</p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;link rel="stylesheet" type="text/css" href="number.css" /&gt; &lt;/head&gt; &lt;body onLoad="focus();number.focus();changeNumber()"&gt; &lt;div id='number' value="1"&gt; &lt;/div&gt; &lt;/body&gt; &lt;footer&gt; &lt;script type="text/javascript" src="number.js"&gt;&lt;/script&gt; &lt;/footer&gt; &lt;/html&gt; </code></pre> <p>JAVASCRIPT</p> <pre><code>var i = parseInt(1); function changeNumber(){ var userInput = i; document.getElementById('number').innerHTML = userInput; } // Capture keyDown events for document document.onkeydown = function(e) { // "34" is the PGUp key if (e.keyCode == 34) { // increment the value in the text input (i++); changeNumber() // "33" is the PGDown key } else if (e.keyCode == 33 &amp;&amp; i &gt; 1) { // decrement the value in the text input (i--); changeNumber() } // "66" is the b key if (e.keyCode == 66){ window.location.reload() } } </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.
    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