user1568811 user1568811 - 1 year ago 59
Javascript Question

Jquery comparing 2 items

I've scoured this site looking for an answer to this question I know I'm going to get reamed for being asked already. Unfortunately none of the answers have worked for me. I have a Magento site that populates a products image and prices on a CMS page. I can pull in everything just fine. The issue I am having is with the prices. Since it's pulling is both sale price and regular price, even if a product isn't on sale it will generate two prices. My question is how do I run a check with jquery and eliminate the sale price if its the same as the regular price?

My Markup/PHP:

<?php $_product = Mage::getModel('catalog/product')->load($this->getData('product_id')); ?>
<div class="priceContainer">
<p id="regularPrice" class="price regular"><?php echo Mage::helper('core')->currency($_product->getPrice()); ?></p>

<p id="salePrice" class="price sale"><?php echo Mage::helper('core')->currency($_product->getFinalPrice(), true, false); ?></p>

my Jquery:

var $j = jQuery.noConflict();
var regPrice = $j('#regularPrice');
var salePrice = $j('#salePrice');
if (salePrice[0] === regPrice[0]) {
$j('#salePrice').css('display', 'none');

Answer Source

Your problem is tht you are comparing the JQuery object. Try comparing like this:

var $j = jQuery.noConflict(); 
var      regPrice = $j('#regularPrice');
var.     salePrice = $j('#salePrice'); 

    if ($(v).text() === salePrice.eq(k).text()){ 
        salePrice.eq(k).css('display', 'none'); 

The explanation is that in your code you are comparing JQuery objects which have more information than the inner text, which is the price.

So, you have to get the text from the JQuery objects. You can do that invoking the .text() method.

WARNING: you should not use the same id more than once. Instead of that you should use a class