Search code examples
javascriptmagentoonclickfavorites

Favorite Item Javascript


I'm having that making a function to add a favorite item, have a heart icon when you click on that it should change the color from gray to red and save the status. Before I had a selection box that could save status but with image unable to adapt a function.

SelectBox where I could save and change status:

 <select id="priority-select-<?php echo $id ?>" class="form-control" name="priority[<?php echo $id ?>]" title="<?php echo $this->__('Priority') ?>">
                                <?php foreach($_hlp->getPriorities() as $k=>$v):?>
                                    <option value="<?php echo $k ?>" <?php if ($k == $item->getPriority()){ echo "selected"; } ?>><?php echo $this->htmlEscape($v) ?></option>
                                <?php endforeach; ?>
                            </select>

I'm trying to do using images for icons:

<img id="heart-l-<?php echo $id ?>"
                                 src="<?php if ($item->getPriority() < 1) {echo $this->getSkinUrl('images/heart-m-disabled.png');} else {echo $this->getSkinUrl('images/heart-m.png');} ?>"
                                 alt=""  onclick='adicionaFav(this.id)'/>

My javascript code:

<script type="text/javascript">
    var $jq = jQuery.noConflict();


    var heart_m = "<?php echo $this->getSkinUrl('images/heart-m.png') ?>";
    var heart_m_disabled = "<?php echo $this->getSkinUrl('images/heart-m-disabled.png') ?>";


    var valor;
    function adicionaFav(id) {

        if(document.getElementById(id).src == "<?php echo $this->getSkinUrl('images/heart-m-disabled.png');?>"){

            document.getElementById(id).src = "<?php echo $this->getSkinUrl('images/heart-m.png');?>";
            priorityChange(1, id);
            $jq('#priority-select-' + id).val('0');

        }
        else{

            document.getElementById(id).src = "<?php echo $this->getSkinUrl('images/heart-m-disabled.png');?>";
            priorityChange(0, id);
            $jq('#priority-select-' + id).val('1');


        }


    }


    function priorityChange(size,id) {



        if(size==1){

            $jq("#heart-m-" + id).attr("src", heart_m);
        }else{

            $jq("#heart-m-" + id).attr("src", heart_m_disabled);
        }/*
         switch (size) {
         case 0: {
         $jq("#heart-m-" + id).attr("src", heart_m_disabled);
         $jq("#heart-l-" + id).attr("src", heart_l_disabled);
         break;
         }

         case 1: {
         $jq("#heart-m-" + id).attr("src", heart_m);
         $jq("#heart-l-" + id).attr("src", heart_l_disabled);
         break;
         }

         case 2: {
         $jq("#heart-m-" + id).attr("src", heart_m);
         $jq("#heart-l-" + id).attr("src", heart_l);
         break;
         }
         }*/
    }
/*
    function prioritySelect(size, id) {
        switch (size) {
            case 0: {
                priorityChange(size, id);
                $jq('#priority-select-' + id).val('0');
                break;
            }

            case 1: {
                priorityChange(size, id);
                $jq('#priority-select-' + id).val('1');
                break;
            }

            case 2: {
                priorityChange(size, id);
                $jq('#priority-select-' + id).val('2');
                break;
            }
        }
    }*/


</script>

note: I could change the color of the heart, however was unable to save the status when upgrading.

can someone tell me what must I do to save the favorite status? want to have prioridad = 0 to gray heart and priority = 1 for the red heart


Solution

  • can solve, following the codes below:

    icon:

                            <img id="heart-m-<?php echo $id ?>"
                                 src="<?php if ($item->getPriority() < 1) {
                                     echo $this->getSkinUrl('images/heart-m-disabled.png');
                                 } else {
                                     echo $this->getSkinUrl('images/heart-m.png');
                                 } ?>"
                                 alt="" onclick='adicionaFav(<?php echo $id ?>)'/>
    
                        </div>
    

    Javascript code:

    <script type="text/javascript">
        var $jq = jQuery.noConflict();
    
        var heart_m = "<?php echo $this->getSkinUrl('images/heart-m.png') ?>";
        var heart_m_disabled = "<?php echo $this->getSkinUrl('images/heart-m-disabled.png') ?>";
    
    
        var valor;
        function adicionaFav(id) {
    
            if (document.getElementById("heart-m-" + id).src == "<?php echo $this->getSkinUrl('images/heart-m-disabled.png');?>") {
    
    
                $jq("#heart-m-" + id).attr("src", heart_m);
                $jq('#priority-select-' + id).val('1');
            }
            else {
    
    
                $jq("#heart-m-" + id).attr("src", heart_m_disabled);
                $jq('#priority-select-' + id).val('0');
            }
    
    
        }
    
    
        function priorityChange(size, id) {
    
    
            if (size == 1) {
    
                $jq("#heart-m-" + id).attr("src", heart_m);
            } else {
    
                $jq("#heart-m-" + id).attr("src", heart_m_disabled);
            }
        }
    
    
    </script>
    

    hope I can to help someone!! =)