tags:

views:

70

answers:

2

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
great! but what if i need only one selected line? Is it possible to set all lines on "unselected" class?
EugenA
@eugenA try my code. toggleCss removes class if it's exists, and adds if it isn't
valya
+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"&gt;&lt;/script&gt;
<!-- 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
is it possible with this solution select only one line at the same time?
EugenA
yeah, try the new version. i've edited the code for it
valya
what should i place in onclick="" in every line-div?
EugenA
nothing. $("#alldivs div").onclick() places it by itself
valya
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
oh i'm sorry. i've forgotten :) not onClick(function() ..., but just click(function()...).
valya
i recommend you to learn jQuery framework, it's very useful, simple, smart and nice
valya