0

i have this piece of code:

getOptionText: function(elem, value, config){
    var text = $(elem).innerHTML;
    var configType = this.getConfigValue(config, 'type', false);
    if (!configType) {
        return text;
    }
    switch (config.type) {
        case 'custom_images':
            var image = this.getConfigValue(config, 'images/' + value, false);
            if (image) {
                text = '<img class="mytest" src="' + image + '" alt="' + text +'" />';
                    jQuery(".mytest").hover(function(){
                        jQuery(this).addClass('big-red-box');

                    },function(){
                      jQuery(this).removeClass('big-red-box');});
            }
            break;

how I can add the image variable on hover? So all i want is when a customer put the mouse over the small images to display the full image.

Thank you

Edit:

    switch (config.type) {
        case 'custom_images':
            var image = this.getConfigValue(config, 'images/' + value, false);
            if (image) {
                text = '<img class="mytest" src="' + image + '" alt="' + text +'" />';
                 $img = $('<img class="mytest" src="' + image + '" alt="' + text + '" />');
                    $img.hover(function () {
                        jQuery(this).addClass('big-red-box');

                    }, function () {
                        jQuery(this).removeClass('big-red-box');
                    });
                jQuery("mytest").append(text);
            }
            break;
1
  • add your markup. Also, where is text defined? provide more details. Commented Nov 12, 2015 at 15:10

1 Answer 1

2

You are never adding the img you create to the DOM. So jQuery(".mytest") will never be able to select it. You need to create the jQuery objec with your html instead of just having the HTMLs text. To the jQuery objec you can bind the hover function.

Finally what you are missing is actually adding the element to the DOM.

var image = this.getConfigValue(config, 'images/' + value, false);
if (image) {
    $img = $('<img class="mytest" src="' + image + '" alt="' + text + '" />');
    $img.hover(function () {
        jQuery(this).addClass('big-red-box');

    }, function () {
        jQuery(this).removeClass('big-red-box');
    });
    // todo: append $img somewhere in the DOM
}

The simplest way to append the $img to the DOM would be:

$("#parentElementId").append($img);

You have a lot of other methods:

Sign up to request clarification or add additional context in comments.

22 Comments

thank you Diego, i am not so good in Jquery, can you tell me or give an example how to append img in the dom?
You have a lot of different ways to append elements to the DOM. Here there are the most common: api.jquery.com/category/manipulation/dom-insertion-inside
so i can add this $("#parentElementId").append($img); in the $img.hover(function () {} ?
You should add it to the DOM outside the hover event (before or after binding it, its the same). If you want in the hover you could .show() it or .hide() it.
can you make this code to fit on my code? i have a lot of errors
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.