JQuery Change Div Background Color Randomly

How to change background color of div by JQuery:



In this post we explain randomly change the back ground color of div at run time.

JQuery Function of change background:

function changecolor() {
        var colors = ["#B40404", "#0000FF", "#FE2E9A", "#FF0080", "#2EFE2E", ];
        var rand = Math.floor(Math.random() * colors.length);
        $('#controls-wrapper').css("background-color", colors[rand]);
        setTimeout('changecolor()', 1000);
    }

JQuery Related Other Post:


JQuery Example of change background:


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="divExample.aspx.cs" Inherits="divExample" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Untitled Page</title>
<script src="http://code.jquery.com/jquery-1.8.2.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        changecolor();
    });
    function changecolor() {
        var colors = ["#B40404", "#0000FF", "#FE2E9A", "#FF0080", "#2EFE2E", ];
        var rand = Math.floor(Math.random() * colors.length);
        $('#controls-wrapper').css("background-color", colors[rand]);
        setTimeout('changecolor()', 1000);
    }
</script>
    <style type="text/css">
        .style1
        {
            text-decoration: underline;
        }
    </style>
</head>
<body>
<form id="form1" runat="server">
<div id="controls-wrapper" align="center">
<p>
<h1><span class="style1">Hello </span> <br class="style1" />
    <span class="style1">Friends </span> </h1>
welcome to this <a href="http://asp-net-by-parijat.blogspot.in/">Asp.net tutorial.</a><br />
    <br />
    <br />
    <br />
&nbsp;</p>
</div>
</form>
</body>
</html>



Other Examples:


Comments

Popular posts from this blog