Search code examples
javascriptjquerykeyup

JQuery parseFloat() always returning "NAN" when passed .text property


I am accessing the text property of an input tag with id currency-converter and trying to convert it to a float value so I can apply some mathematical transformation to convert it to USD then make the <h3> tag with id label display the value.

The only problem is parseFloat() is returning NAN even if the only value in in the input is numeric. 4 or 55 for example.

Why is parseFloat returning NAN?

<script>
    $(function(){
        $("#currency-converter").keyup(function(){
            $('#label').text(parseFloat($("#currency-converter").text) + " BTC-ICO");
        });
    });
</script>

Solution

  • $("#currency-converter").text will return the method, so parseFloat will give NaN. you need to call the method using $("#currency-converter").text(). To get the value inside input box you can use $("#currency-converter").val()