I have many lines on my page generated from database with PHP. Each line is in DIV. I'd like to "select" a line by clicking on it. "Select" means change css for it. What is the easiest way to do it?
A:
You could do just a onclick event and change the css class;
<div class="yourCss" onclick="this.className='clickedCss';">content</div>
Björn
2009-09-24 08:27:44
great! but what if i need only one selected line? Is it possible to set all lines on "unselected" class?
EugenA
2009-09-24 08:33:18
@eugenA try my code. toggleCss removes class if it's exists, and adds if it isn't
valya
2009-09-24 08:36:15
+2
A:
smarter way is to use js frameworks, like jQuery:
<div id="alldivs">
<div>...</div>
<div>...</div>
<div>...</div>
</div>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
<!-- you don't need to download anything, just add this line ;-) -->
<script>
$(function(){
$("#alldivs div").click(function(){
$("#alldivs div").removeClass('clickedCss');
$(this).addClass('clickedCss');
});
});
</script>
valya
2009-09-24 08:32:26
it doesn't work. I have it as you told: <div id="alldivs"><div>...</div><div>...</div></div>both script sections i placed in html head area.
EugenA
2009-09-24 09:23:17
oh i'm sorry. i've forgotten :) not onClick(function() ..., but just click(function()...).
valya
2009-09-24 09:45:44
i recommend you to learn jQuery framework, it's very useful, simple, smart and nice
valya
2009-09-24 09:47:30