1

I am trying to use this script:

<script>
var original_value = Number($('#ProductPrice').text().replace(/[^0-9.]+/g,""));

$(document).on('keyup', '#ProductPrice', function() {
if (Number($('#ProductPrice').text().replace(/[^0-9.]+/g,"")) > original_value) {
    $("#ProductPrice").css("color","red");
} else {
    $("#ProductPrice").css("color", "black");   
}
});
</script>

I keep getting an error:'undefined' is not a function (evaluating '$(document).on')

Help?

user2687646
  • 264
  • 1
  • 5
  • 12

3 Answers3

2

I have a feeling you have jQuery in no conflict mode, which means the $ is not assigned to jQUery

You can call your code like this:

(function ($) {
    var original_value = Number($('#ProductPrice').text().replace(/[^0-9.]+/g, ""));
    $(document).on('keyup', '#ProductPrice', function () {
        if (Number($('#ProductPrice').text().replace(/[^0-9.]+/g, "")) > original_value) {
            $("#ProductPrice").css("color", "red");
        } else {
            $("#ProductPrice").css("color", "black");
        }
    });
})(jQuery)

You can also just replace all the $ with jQuery;

Sergio
  • 28,539
  • 11
  • 85
  • 132
0

Assuming that you are taking the value as input from user in a text box. You need to have a value and not text for the input type text.

var original_value = Number($('#ProductPrice').val().replace(/[^0-9.]+/g,""));

$(document).on('keyup', '#ProductPrice', function() {
if (Number($('#ProductPrice').val().replace(/[^0-9.]+/g,"")) > original_value) {
    $("#ProductPrice").css("color","red");
} else {
    $("#ProductPrice").css("color", "black");   
}
});

This works Fine for me.

Akshay Khandelwal
  • 1,570
  • 11
  • 19
  • Just FYI, the OP has a string of questions all related to the same problem. This was supposed to be on a `contenteditable` element but the OP left it out. – musicnothing Sep 13 '13 at 20:22
0

Seems like you might be loading jQuery in no-conflict mode, or not loading it at all.

See this question & my answer to it:

jQuery Uncaught TypeError: Property '$' of object [object Window] is not a function

Community
  • 1
  • 1
Julian
  • 2,814
  • 21
  • 31