Click here to Skip to main content
15,906,567 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi..i hav a requirement that : a page has three divs.Upon div click its background color to be changed and its content to be displayed in a span.

i tried as below:
JavaScript
("#div").click{
$(this).removeclass(x);
$(this).addclass(y);
$(#span).html("content");
}

But this is not restoring divs original color after another div is clicked
Plz help me

if i has same class to each div there is no issue...but my requirement is
i hav different class for each div...how to solve this ...help me
Posted
Updated 5-Dec-13 0:19am
v3

Hi try like this..

XML
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
    <script src="JQuery.js" type="text/javascript"></script>
    <style type="text/css">
        .divclass
        {
            background-color: Red;
        }
        .divclickedclass
        {
            background-color: green;
        }
    </style>
    <script type="text/javascript">

        $(function () {
            $('#div1,#div2,#div3').click(function () {
                $('#div1,#div2,#div3').removeClass('divclickedclass').addClass('divclass');
                $(this).removeClass('divclass').addClass('divclickedclass');
            });

        });

    </script>
</head>
<body>
    <form id="form1" runat="server">

    <div id="div1" class="divclass" style="width: 100px; height: 100px; border: 1px solid red">
        div1</div>
    <div id="div2"class="divclass" style="width: 100px; height: 100px; border: 1px solid green">
        div2</div>
    <div id="div3" class="divclass" style="width: 100px; height: 100px; border: 1px solid blue">
        div3</div>
    </form>
</body>
</html>
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 1-Dec-13 23:32pm    
It does show the principle, a good idea to use the descriptor allowing to set the same event handle to all elements; my 5. OP's mistake is incorrect syntax and not caring about click on other divs.
—SA
Try this too:

XML
<!DOCTYPE HTML>
<html>
<head>
<title>A Code Project Solution</title>
<style>
.blue {
   background: #00f;
}
</style>
<script src="jquery.min.js"></script>
<script>
var prev;

$(document).ready( function() {

$('div').click( function() {
    $(this).toggleClass("blue");
    $(prev).toggleClass("blue");
    prev=this;
    $('#myspan').html($(this).html());
  } );
});
</script>
</head>

<body>
<div>Content from div 1</div>
<div>Content from div 2</div>
<div>Content from div 3</div>
<span id="myspan">No div is being clicked yet.</div>
</body>
</html>
 
Share this answer
 
XML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<script type="text/javascript" language="javascript">
    $(function() {
        $('div').addClass('divcolor');
        $('div').click(function() {
        $('div').removeClass('divnewcolor').addClass('divcolor');
            $(this).removeClass('divcolor').addClass('divnewcolor');
        });
    });
</script>
<style type="text/css">
.divcolor
{
    background-color:Lime;
}
.divnewcolor
{
    background-color:Green;
}
</style>
</head>
<body>
    <form id="form1" runat="server">

    <div>div1</div>
    <br />
    <div>div2</div>
    <br />
    <div>div3</div>

    </form>
</body>
</html>



Try this and your problem will be resolved.
 
Share this answer
 

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