tags:

views:

50

answers:

3

no i have this code

<a href="#">
<span style="border-right: medium none;" class="Yes">Yes</span>
</a>
<a href="#">
<span class="No">No</span>
</a>

if no is current i want when click on yes class calling current adding on yes span and delete current class from no

if yes current i want when click on no class calling current adding on no span and delete current class from yes

i want each case happen in just one click please help me

+1  A: 

Here is a sample. This is a first cut, may be this can be optimized. All I am trying to do is remove current class from all the elements and add it to current one.

You have to modify your html to have a unique class

    <a href="#">
    <span style="border-right: medium none;" class="Yes yesno">Yes</span>
</a>
<a href="#">
    <span class="No yesno">No</span>
</a>

This is the jQuery code

        $('.yesno').click(function(){
            $('.yesno').each(function(i, v){
                $(v).removeClass('current');
            });
            $(this).addClass('current');
        });

see if it helps..

Teja Kantamneni
this is great idea
moustafa
I don't understand the `$('.yesno').each(function(i, v){ $(v).removeClass('current'); });` Why not `$('.yesno').removeClass('current');` ?
Nick Craver
@Nick Craver, Yes it can be done directly with out a loop, I started doing something else and was left like that. I also mentioned in the post that this can be optimized further.
Teja Kantamneni
+2  A: 

Try this:

$(".Yes, .No").click(function() {
  if(!$(this).hasClass("current"))
    $(".Yes, .No").toggleClass("current");
});

Yes and No both fire the click, but the action only happens if the click didn't happen on the current one. Just set current to whatever you want initially, the toggle will take care of it from then on as you describe in the question.

Nick Craver
Clever solution, but what happens if both of them doesn't have a class `current` initially?
Teja Kantamneni
@Teja - I assume based on the question one would initially. If you read the text of the question, in each action case it should only happen if the *other* element has the `current` class. If you can't do it server side, then you could default to no on everything like this: `$(".No").addClass("current");`
Nick Craver
+1  A: 

Perhaps I am misinterpreting your question, but it looks like you're trying to produce something that behaves like Radio Buttons.

<div class="radioButtons">
    <a href="#">Yes</a>
    <a href="#">No</a>
    <a href="#">Maybe</a>
</div>
<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript">
    function select(event) {
        var clicked = $(event.target);
        clicked.siblings('a').removeClass('selected');
        clicked.addClass('selected');
    }

    $(function() {
        $('.radioButtons a').click(select);
    });
</script>
ESV