I would like to change the style on a div with an onclick… and remove the style when clicking somewhere outside of the div.
I have the following code setup… can someone help me to remove the style on the divs if you click anywhere else on the page?
<head>
<style type="text/css">
.account{
width: 100px;
height: 100px;
border: 1px solid #000;
}
.selected{
border: 2px solid #F00;
}
</style>
<script type="text/javascript">
$(document).ready(function(){
$(".account").click(function(){
$(".selected").removeClass("selected");
$(this).addClass("selected");
});
});
</script>
</head>
<body>
<h1>the test</h1>
<div class="account">test 1</div>
<div class="account">test 2</div>
</body>
Thank you very much for any help you can give me!!!
Try this.
Working demo – http://jsfiddle.net/yLhsC/
Note that you can use
onordelegateto handle click event onaccountelements if there are many on the page.Something like this.
Using
onif using jQuery 1.7+Using
delegate