Note that there are some explanatory texts on larger screens.

plurals
  1. POJavaScript retrieving a innerHTML value from a class that uses multiple tags
    primarykey
    data
    text
    <p>So I'm building a JavaScript calculator, and I have the class numerical, and whenever that class gets click it iterates a code sequence as you can see in my javascript below, but it won't retrieve the innerHTML value of the one it clicked for some reason. Do I need to move on to ID's or can I make this work?</p> <p>JavaScript</p> <pre><code>$(document).ready(function(){ $('.numerical').on('click', function(){ var innerDisp = document.getElementById('display').innerHTML; var num = document.getElementById(this).innerHTML; document.getElementById('display').innerHTML = innerDisp + num; }); }); </code></pre> <p>HTML</p> <pre><code>&lt;body&gt; &lt;center&gt; &lt;div id="calculator"&gt; &lt;div id="display"&gt;&lt;/div&gt; &lt;button class="numerical" id="0"&gt;0&lt;/button&gt; &lt;button class="numerical" id="1"&gt;1&lt;/button&gt; &lt;button class="numerical" id="2"&gt;2&lt;/button&gt; &lt;button class="function" id="add"&gt;+&lt;/button&gt; &lt;button class="numerical" id="3"&gt;3&lt;/button&gt; &lt;button class="numerical" id="4"&gt;4&lt;/button&gt; &lt;button class="numerical" id="5"&gt;5&lt;/button&gt; &lt;button class="function" id="subtract"&gt;-&lt;/button&gt; &lt;button class="numerical" id="6"&gt;6&lt;/button&gt; &lt;button class="numerical" id="7"&gt;7&lt;/button&gt; &lt;button class="numerical" id="8"&gt;8&lt;/button&gt; &lt;button class="function" id="multiply"&gt;x&lt;/button&gt; &lt;button class="numerical" id="9"&gt;9&lt;/button&gt; &lt;button class="function" id="c"&gt;C&lt;/button&gt; &lt;button class="function" id="ce"&gt;CE&lt;/button&gt; &lt;button class="function" id="divide"&gt;/&lt;/button&gt; &lt;/div&gt; &lt;/center&gt; &lt;script src="calculator.js"&gt;&lt;/script&gt; &lt;/body&gt; </code></pre>
    singulars
    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.
    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