Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'undefined' is not an object

I have a div that looks like this:

<em id="ProductPrice" class="ProductPrice VariationProductPrice">$75.00</em>

And I need to make the color of the text change color if there is a change to it's value.

I wrote this:

<script>

$(document).ajaxSuccess(function(){

    var currentPrice = $.trim($("#ProductPrice").text());

    if(currentPrice == "%%GLOBAL_ProductPrice%%") 
    {
            $("#ProductPrice").style.color="black";
            console.log("black");
    }
    else
    {
            $("#ProductPrice").style.color="red";
            console.log("red");
    }

});

%%GLOBAL_ProductPrice%% is a variable in our CMS that gives the base value before any change occurs.

I get the error 'undefined' is not an object (evaluating '&("#ProductPrice").style.color="red"')

What am I doing wrong?

like image 398
user2687646 Avatar asked Aug 29 '26 16:08

user2687646


1 Answers

You are using jQuery selectors in plain Javascript, won't work. In javascript you can change the property style, in jQuery you have a method .css() to do this.

Try this instead:

$("#ProductPrice").css('color','black');

or plain javascript

document.getElementById('ProductPrice').style.color="black";
like image 155
Sergio Avatar answered Sep 01 '26 05:09

Sergio



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!