Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h3>Regarding Method 1:</h3> <p>I would need to see a bit more of your html structure to say for sure, but it sounds like in Method 1, the function isn't being declared properly in a way that is in scope. That might have to do with the names being the same, as <a href="https://stackoverflow.com/a/15949644/259014">theBrain mentioned</a> or it might caused by some other problem.</p> <p>Edit: From your response to theBrain, it sounds like you are able to get method 1 to work if you use different names. Given that, you can also prevent the page post by changing the onclick to include a return false value. Either of the following will work:</p> <pre><code>&lt;button id="buttonAdd" onclick="add(); return false;"&gt;Add&lt;/button&gt; </code></pre> <p>or </p> <pre><code>&lt;button id="buttonAdd" onclick="return add();"&gt;Add&lt;/button&gt; </code></pre> <p>coupled with the addition of <code>return false;</code> as the last line of your <code>add()</code> function's code.</p> <h3>Regarding Method 2:</h3> <p>In either case, method 2 is a better way of implementing this, so we can sort of ignore the reasons behind method 1 failing (though having distinctly different names for the function vs the button element would certainly be a good practice; personally, I preface all of my button ids with 'btn_').</p> <p>The likely reason for the super-fast clearing of the console in both methods is that you do not have a <code>type</code> declared for the button. Different browsers do things differently in the absence of a type (see the tip on the <a href="http://www.w3schools.com/tags/tag_button.asp" rel="nofollow noreferrer">W3Schools Button Tag</a>), and it sounds like yours is treating this as a submit button, which means that it posts back to the page when clicked. You should be able to prevent this behavior by specifying <code>type='button'</code> within the attributes of the button element.</p> <h3>Regarding Method 3:</h3> <p>Finally, method 3 is providing the behavior that it is because your assignment statement is also executing a call to the <code>add()</code> function.</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. VO
      singulars
      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