Why is this function not working?

1

I'm a layman and I'm learning Javascript for a simple hybrid app . The first catch is a function that is not executing:

The said whose:

function calcular(){
    //ap1 = parseFloat(document.getElementById('ap1').value);
    var media, final;

    media = calculo.ap1.value.replace(",", ".");
    media = parseFloat(media);
    alert(media); 
}

I referenced the file where it is in the index and made the call in a form:

<form action="javascript:calcular();" method="get"id="calculo" name="calculo" class="list">

Even so, it is not running the function. Thanks for the help.

    
asked by anonymous 07.01.2017 / 18:23

1 answer

2

Instead of putting your function in the action put in the attribute specific to the javascript event.

I made this example by imagining that the data that will be called is correct in the other functions.

<form action="#" onsubmit="calcular()" method="get"id="calculo" name="calculo" class="list">
    <button type="submit" value="Submit">Submit</button>
</form>

Js

function calcular()
{
       var valor, qtd,  media;

       valor = 10;
       qtd = 3;
       media = valor / qtd;
      console.log(media); 
}
    
07.01.2017 / 18:40