Click here to Skip to main content
15,904,153 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi 2 all

i have 8 div tags each of them have id a1 and the next have id of a2 and third have id of a3

and so on each have color

and i also have container div for my web site which have class
.pro_wrapper



i need when user select div of a1 for example change the color of the wrapper of the site to a1 color my code is below didn't work


C#
for(var i=1;i<=8;i++)
       {

        var color=$("#"+a(i));
        $(color).click(function()
        {


        switch(color)
        {
            case (i==1):
            $(".pro_wrapper").css("background-color","red");
            break;
            case (i==2):
            $(".pro_wrapper").css("background-color","yellow");
            break;
        }
        });
       }

note that i already write $(document).ready line

what's wrong with my code
Posted

1 solution

HTML
<!DOCTYPE html />
<html>
<head>
    <title>Untitled Page</title>
    <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <style type="text/css">
        .wrapper
        {
            width: 200px;
            height: 200px;
        }

        .color
        {
            width: 20px;
            height: 20px;
        }

        .red
        {
            background-color: #f00;
        }

        .green
        {
            background-color: #0f0;
        }

        .blue
        {
            background-color: #00f;
        }
    </style>
</head>
<body>
    <div class="wrapper"></div>
    <div class="color red" id="a1"></div>
    <div class="color green" id="a2"></div>
    <div class="color blue" id="a3"></div>

    <script type="text/javascript">
        $(document).ready(
            function () {
                for (var i = 1; i <= 3; i++) {
                    $("#a" + i).click(function () {
                        $(".wrapper").css("background-color", $(this).css("background-color"));
                    });
                }
            }
        );
    </script>
</body>
</html>
 
Share this answer
 
v2
Comments
TheSniper105 18-Nov-13 8:50am    
i need to use loop if i have 255 color do i have to type their id Manually؟ i know the static method you typed but i need it dynamic
Kornfeld Eliyahu Peter 18-Nov-13 8:59am    
see update...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900