Why isn’t this Function working?

Asked

Viewed 48 times

1

I am layman and am learning Javascript for a app simple hybrid. The first obstacle is a Function that is not running:

The one whose name:

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’s not running Function. I appreciate the help.

  • 2

    And what comes in calculo.ap1.value?

  • It is the value of an imput within the form

  • 2

    Can you show us the value of it? Put a console.log(calculo.ap1.value).

  • Decided to take the action 'Function' and put in 'Onsubmit' .. Thanks friends!!

1 answer

2


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

I made this example imagining that the data that will be called are 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); 
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.