1

I read this thread: Simple Percentage Calculation of an Input Field with JQuery

But can't seem to get it to work for my situation. I have two input boxes, Wholesale and Sell Price, and I want to calculate the markup (difference) on the fly as the user is changing their Sell Price. I've built a simplified version of what I'm trying to do here:

http://jsfiddle.net/schuss/rp0brqj1/2/

And below is the JS - can anyone see what I'm doing wrong? Thanks!!

$(function() {
    
    // cache elements that are used at least twice
    var $sellprice = $("#SellPrice"),
        $markup = $("#markup"),
        $wholesale = $("#wholesale");
    
    // attach handler to input keydown event
    $sellprice.keyup(function(e){
        if (e.which == 13) {
            return;
        }
        var sellprice = parseFloat($sellprice.val()),
            markup = sellprice-wholesale;
        
        if (isNaN(sellprice)) {
            $markup.hide();
            return;
        }
        else {
        
        $markup.fadeIn().text(markup.toFixed(2));


        }
    });
   
});
Community
  • 1
  • 1
BNash
  • 11
  • 1
  • Can you make a fiddle so we can see the code in action – StudioTime Mar 25 '15 at 07:02
  • Updated fiddle - i found a small error (that didn't materially change anything, was just a red herring) http://jsfiddle.net/rp0brqj1/4/ – BNash Mar 25 '15 at 07:03
  • Are you actually trying to use AngularJS? Because everything you have done is only using jQuery. – GregL Mar 25 '15 at 07:24

2 Answers2

0

You want to set the value of input field, so in this case you need to use $.fn.val method:

$markup.fadeIn().val(markup.toFixed(2));

Demo: http://jsfiddle.net/rp0brqj1/6/

dfsq
  • 191,768
  • 25
  • 236
  • 258
0

Try this (I haven't included the else - and don't forget to include JQuery in your fiddles!):

$(function() {

  var $sellprice = $("#SellPrice"),
    $markup = $("#markup"),
    $wholesale = $("#Wholesale");
  $sellprice.on('keyup', function(){
     $markup.val(parseFloat($sellprice.val()-$wholesale.val()))
  });

});

Fiddle

StudioTime
  • 22,603
  • 38
  • 120
  • 207