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?
$("#dialog-tip"). Really, just do as little "DOM touching" as possible. – Mark Oct 1 '12 at 17:07