Note that there are some explanatory texts on larger screens.

plurals
  1. POIs there a simple and efficent way to make JS works correctly with forms?
    primarykey
    data
    text
    <p>i asked a similar question yesterday and the answers were very clear so i thought that i understand the concept. This morning i set up a new HTML form and write a little javascript code snippet to make it work. But again nothing, i tried variuous implementation, but i get no response. i tried with the <code>action</code> attribute, i tried with the onclick, i looked on the references and everything look fine to me. I've just started studying programming, so i may missing something stupid, but i need help. </p> <p>here is my code:</p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;title&gt;PROVA&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;form action="javascript: eseguiMedia();" &gt; Proponi un numero per ogni campo:&lt;br&gt; &lt;input type="text" id="numero1" &gt;&lt;br&gt; &lt;input type="text" id="numero2" &gt;&lt;br&gt; &lt;input type="text" id="numero3" &gt;&lt;br&gt; &lt;input type="submit" value="Submit" &gt; &lt;/form&gt; &lt;script type="text/javascript"&gt; function eseguiMedia() { var value1 = document.getElementsById("numero1").value; var value2 = document.getElementsById("numero2")value; var value3 = document.getElementsById("numero3").value; var average = Media(value1,value2,value3); average = Math.round(average * 100); average = average / 100; alert("La media di " + value1 + ", " + value2 + " and " + value3 + " \(rounded to two places\) è " + average + "."); } function Media(num1,num2,num3) { return ( (num1*1) + (num2*1) + (num3*1) ) / 3; } &lt;/script&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>i tried this different solutions:</p> <ul> <li>using the action attribute in this way action="javascript: eseguiMedia();"</li> <li>without the action, but with the onclick="eseguiMedia();" in the button. . with both of them.</li> </ul> <p>i would like to know what is the correct and always working way to invoke a function in a HTML form and pass to it the values. </p> <p>ps(i know jquery is better, but i need to use javascript in this exam) </p>
    singulars
    1. This table or related slice is empty.
    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