I am currently developing a jQuery Plugin for a basic game. The game inserts all required HTML into the div where it is called and links its CSS file to the header. However, I have encountered an issue when trying to reload only specific elements, such as jQuery UI Sliders, after injecting them. If I use location.reload after the injection, it refreshes the page to its original state without the added HTML. On the other hand, if I don't call it, everything displays correctly except for the sliders. Is there a way to reload just those sliders so they appear after being dynamically injected? (The HTML works perfectly fine when pasted directly into the test HTML file body; the problem arises when inserting it dynamically, like when triggering a button click.)
Below is some of the code snippet that I am using to achieve this: // Load the HTML and CSS. $("head").append( ''); $("body").append(''); $("#load-button").remove();
$("#game").append(
"<!-- Generated Swatch-->\n" +
"<div id='generatedSwatch' class ='ui-widget-content ui-corner-all'></div> </br>\n" +
"<!-- Score -->\n" +
"<p id='score'>Score: 0</p>\n" +
"<!-- Sliders -->\n" +
"<div class='slider-row'>\n" +
" <div id='red'></div>\n" +
" <input type='text' id='rval' class='input-box'></input> </br>\n" +
"</div>\n" +
"<div class='slider-row'>\n" +
" <div id='green'></div>\n" +
" <input type='text' id='gval' class='input-box'></input> </br>\n" +
"</div>\n" +
"<div class='slider-row'>\n" +
" <div id='blue'></div>\n" +
" <input type='text' id='bval' class='input-box'></input> </br>\n" +
"</div>\n" +
"<!-- Got It Button -->\n" +
"<button id='got-it' onclick='calculateScore()' class='button'>Got It</button>\n" +
"<!-- Picked Swatch -->\n" +
"<div id='pickedSwatch' class='ui-widget-content ui-corner-all'></div>\n" +
"<!-- Percent Error -->\n" +
"<p id='error'>Percent Error: </p>\n" +
"<!-- Next Button -->\n" +
"<button id='next' onclick='generateSwatch()' class='button'>Next</button>\n" +
"<!-- Game Over Message -->\n" +
"<p id='game-over'>Game Over</p>\n" +
"<!-- Play Again -->\n" +
"<button id='play-again' onclick='playAgain()' class='button'>Play Again</button>"
);
// Hide Elements.
$("#pickedSwatch").hide();
$("#error").hide();
$("#next").hide();
$("#game-over").hide();
$("#play-again").hide();
// Make Sliders Work
$("#red,#green,#blue").slider(
{
orientation: "horizontal",
range: "min",
max: 255,
value: 127,
slide: refreshSwatch,
change: refreshSwatch
});
$("#red").slider( "value", 255);
$("#green").slider( "value", 140);
$("#blue").slider("value", 60);
// Code to move slider when box changed.
$("#rval").change(function()
{
$("#red").slider("value", $(this).val());
});
$("#gval").change(function()
{
$("#green").slider("value", $(this).val());
});
$("#bval").change(function()
{
$("#blue").slider("value", $(this).val());
});
// Generates the swatch for the first time and sets the turns.
generateSwatch();