Tariq Sherriff Tariq Sherriff - 4 months ago 10
HTML Question

How do i wrap my script in a $( document ).ready(function() {

I need help with wrapping my code in a ready function. Below is my code:

---------- Here's my code -----------------

var color = "White";
$("#btnBlue").hover(function () { color = "Blue" });
$("#btnGreen").click(function () { color = "Green" });
$("#btnYellow").click(function () { color = "Yellow" });
$("table tr td").click(function () { $(this).css("background-color", color); });


---------- I want to wrap the code into the function below-----------------

$( document ).ready(function() {

// Your code here.

});


I greatly appreciate any help you can give.

Answer

You can simply do this:

$( document ).ready(function() {

var color = "White";
$("#btnBlue").hover(function () { color = "Blue" });
$("#btnGreen").click(function () { color = "Green" });
$("#btnYellow").click(function () { color = "Yellow" });
$("table tr td").click(function () { $(this).css("background-color", color); });

});