Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I am very new to JQuery and am not familiar with all its methods. How would you refactor showNextTip() method (it looks very ugly to me)

    <div id="dialog-tip" title="Did You Know?">
        <p>I am tip 1</p>
        <img src="Images/Tips/tip1.png" alt="Tip!" />     
    </div>

function showNextTip() {
    var tip1_title = "I am tip 1";
    var tip1_p = "<p>" + tip1_title + "</p>";
    var tip1_img = "<img src='Images/Tips/tip1.png' alt='Tip!' />";

    var tip2_title = "I am tip ";
    var tip2_p = "<p>" + tip2_title + "</p>";
    var tip2_img = "<img src='Images/Tips/tip2.png' alt='Tip!' />";

    var tip3_title = "I am tip 3";
    var tip3_p = "<p>" + tip3_title + "</p>";
    var tip3_img = "<img src='Images/Tips/tip3.png' alt='Tip!' />";

    if ($("#dialog-tip p").html() == tip1_title) {
        $("#dialog-tip p").replaceWith(tip2_p);
        $("#dialog-tip img").replaceWith(tip2_img);
    } else if ($("#dialog-tip p").html() == tip2_title) {
        $("#dialog-tip p").replaceWith(tip3_p);
        $("#dialog-tip img").replaceWith(tip3_img);
    } else if ($("#dialog-tip p").html() == tip3_title) {
        $("#dialog-tip p").replaceWith(tip1_p);
        $("#dialog-tip img").replaceWith(tip1_img);
    }
}

EDIT: I might have different strings for Title and Image. in c# i would of create a class named Tips with 2 properties: title and image. how can i do so with JQuery?

share|improve this question
show us your own refactoring code for this – Stefan P. Oct 1 '12 at 17:03
i would like to see other implementations of showNextTips(). Thanks. – user829174 Oct 1 '12 at 17:06
Well, here are a couple tips: DOM manipulation is expensive. Put all the content in the DOM, then just toggle which "tip" is active using CSS classes. Cache references to DOM elements (anything like $("#dialog-tip"). Really, just do as little "DOM touching" as possible. – Mark Oct 1 '12 at 17:07
Thank you for your answer Mark. can you share a code example? – user829174 Oct 1 '12 at 17:09

migrated from stackoverflow.com Oct 2 '12 at 12:35

3 Answers

<div id="dialog-tip" title="Did You Know?">
    <p class="tip-text">I am tip 1</p>
    <img class="tip-image" src="/images/tips/tip1.png" alt="">
</div>

(function() {
    var tips = ['I am tip 1', 'I am tip 2', 'I am tip 3'],
        tipIndex = 0;

    window.showNextTip = function() {
        tipIndex = (tipIndex + 1) % tips.length;
        $('#dialog-tip .tip-text').text(tips[tipIndex]);
        $('#dialog-tip .tip-image').prop('src', '/images/tips/tip' + (tipIndex+1) + '.png');
    };
})();
  • Avoid duplication.
  • Avoid pollution of the global name space.
  • Reference elements by classes rather than tag names to improve maintainability.
  • Always escape HTML to avoid XSS and hidden bugs.
  • Minimize DOM touches, because they can be expensive.
share|improve this answer
var tips = {
    1: "I am tip 1",
    2: "I am tip 2",
    3: "I am tip 3",
}; 

var addressOfIMG = {
    1: "I am add 1.png",
    2: "I am add 2.bmp",
    3: "I am add 3.jpg",
}; 

$.fn.NewIndex = function(oldIndex) {
    (oldIndex == 1) ? return 2 : (oldIndex == 2) ? return 3 : return 1;
}    

currentIndex = 1;           //global var that will keep track of the tip index

$.fn.showNextTip = function () {    

    var base_address = "Images/Tips/tip";
    var dialogTip = $("#dialog-tip p").innerHTML;

    var newIndex = NewIndex(currentIndex);

    $("#dialog-tip p").innerHTML = tips[newIndex];
    $("#dialog-tip img").attr(addressOfIMG[newIndex]);

    currentIndex = newIndex;
}

Next, just simple run your code, in ready or whichever event you like, as:

$.(document).ready(function() {    //this could be any other event besides ready()
    $.fn.showNextTip();
}
share|improve this answer
select only once: var p = $("#dialog-tip p"); – Stefan P. Oct 1 '12 at 17:14
How about now? @StefanP. – Sean Vaughn Oct 1 '12 at 17:36
how can i put both the title and the image in the array you put? please see my edit to the original question – user829174 Oct 1 '12 at 17:37
You don't to make a class here in jQuery, it would just lead to more complications, instead, just use the above code. tips is the global array of constant strings that has all of your tips with their indexes. Those indexes will help you track the image and thus, you don't need the class to do that for you. :) @user829174 – Sean Vaughn Oct 1 '12 at 17:41
but i will need to have 2 arrays. one for the title and one for the images (i might have different names for the images) and they should be tied up as they are related. no? – user829174 Oct 1 '12 at 17:44
show 2 more comments

For all <div id="dialog-tip" class="dialog-tip" title="Did You Know?">

Give it a common class Name.. Then Try this

var tip_title = "I am tip " ;

var tip_p = [];
var tip_img = [];

var dialogTips = $('.dialog-tip');

$.each(dialogTips , function(i) { 

     tip_p[i] = title_tip +  i+1;
     tip_img[i] = "<img src='Images/Tips/tip" +  i+1 + ".png' alt='Tip!' />";

});

$.each(dialogTips , function(i) { 
    if( i == dialogTips .length){
        $(this).find('p').remove().append( tip_p[0] );
        $(this).find('img').remove().append( tip_img[0] );
    }
    else if( i < dialogTips .length){
        $(this).find('p').remove().append( tip_p[i++] );
        $(this).find('img').remove().append( tip_img[i++] );
    }
});

DEMO

This should work for any number of dialogTips in your code..

share|improve this answer
Thats a good answer :) but what do i do if the title is different that "I am tip X" ? – user829174 Oct 1 '12 at 17:21
Then store the values that are different in a separate array.. var arr = ['X','Y','Z','A'].. Then instead of appending i append arr[i] – sushanth reddy Oct 1 '12 at 17:25
in c# i would tied it up to a class named Tips with 2 properties: title and image. can you please share an example how can i do it with JQuery? – user829174 Oct 1 '12 at 17:33
Check this jsfiddle.net/sushanth009/fa8VX – sushanth reddy Oct 1 '12 at 17:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.