javascript - Remove "NaN" keyword from undefined object output -




please check code bellow. on modal opening output value displays "nan" until provide input. want display blank field on output until provide input. how can achieve that? possible? happy coding all. please add bootstrap , jquery test code. thanks.

<!doctype html> <html> <head>     <title>test</title> </head> <body>  <!-- trigger modal button --> <button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#mymodal">open modal</button>  <!-- modal --> <div id="mymodal" class="modal fade" role="dialog">   <div class="modal-dialog">      <!-- modal content-->     <div class="modal-content">       <div class="modal-header">         <button type="button" class="close" data-dismiss="modal">&times;</button>         <h4 class="modal-title">modal header</h4>       </div>       <div class="modal-body">         <input type="text" name="" id="amazonprice" disabled="disabled" value="10"><br/>         <input type="text" name="" id="breakeven" disabled="disabled" value="20"><br/>         <input type="text" name="" id="comprice" disabled="disabled" value="15"><br/>         input:<input type="text" name="" id="yourprice" value=""><br/>         output:<input type="text" name="" id="yourprofit" value=""><br/>       </div>       <div class="modal-footer">         <button type="button" class="btn btn-default" data-dismiss="modal">close</button>       </div>     </div>    </div> </div>   <script type="text/javascript">      $('#yourprice').keyup(updatetxt);     $('#amazonprice').keyup(updatetxt);      function updatetxt() {         var yourprice = $('#yourprice').val();         var amazonprice = $('#amazonprice').val();         var breakeven = $('#breakeven').val();          var float_yourprice = parsefloat(yourprice);         var float_amazonprice = parsefloat(amazonprice);         var float_breakeven = parsefloat(breakeven);          $('#yourprofit').val(profitcalculation(float_amazonprice, float_breakeven, float_yourprice));     }     updatetxt();      function profitcalculation(amazonprice, breakeven, yourprice) {         var yourprofit = yourprice - (yourprice / 100 * breakeven + 0.30) - amazonprice;         return yourprofit.tofixed(2);     }  </script> </body> </html> 

quick solution: insert conditional check if it's nan , return empty string if so.

// inside function profitcalculation return isnan(yourprofit) ? '' : yourprofit.tofixed(2); 

isnan - https://developer.mozilla.org/en/docs/web/javascript/reference/global_objects/isnan





wiki

Comments

Popular posts from this blog

Asterisk AGI Python Script to Dialplan does not work -

python - Read npy file directly from S3 StreamingBody -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -