0

I have this toggle button in which when i click on it the button shows that it is active and the css changes so it is darker. but when i click the other button the toggle doesn't deactivate (on/off) i want to change that so that it turns on when i click on it and off when i click on the other button.

buttons

<div class="btn-group float_right">
    <a id="gridBtn" onclick="$.views.stuff.ToggleView('gridView')" title="View as grid" class="btn" href="#"><i class="icon-th-large"></i></a>
    <a id="listBtn" onclick="$.views.stuff.ToggleView('listView')" title="View as list" class="btn" href="#"><i class="icon-th-list"></i></a>
</div>

javascript

$(document).ready(function () {
        $('a#gridBtn').click(function () {
            $(this).toggleClass("down");
        });
        $('a#listBtn').click(function () {
            $(this).toggleClass("down");
        });
    });
2
  • can you show some problem in Jsfiddler Commented Apr 11, 2013 at 13:00
  • 1
    One advice is to remove the onlick from your html and put them in the $("...).click part. Commented Apr 11, 2013 at 13:04

3 Answers 3

3

Try this CSS trick:

<div class="btn-group float_right">
    <label>
        <input type="radio" name="viewbuttons" />
        <a id="gridBtn" .....>...</a>
    </label>
    <label>
        <input type="radio" name="viewbuttons" />
        <a id="listBtn" .....>...</a>
    </label>
</div>

Then add this CSS:

.btn-group input {display:none}
.btn-group a { /* your "deselected" styles here */ }
.btn-group input+a { /* your "selected" styles here */ }

This is extremely efficient since you don't need any jQuery to make it work ;)

Sign up to request clarification or add additional context in comments.

2 Comments

very handy i will give it a try and see how it works. Thanks for trick.
If you're not bothered about the semantics of your HTML that is.
2

You can give your #gridBtn the down class by using:

$('#gridBtn').addClass('down');

Also, there's no need for two separate click handlers:

  $('a#gridBtn, a#listBtn').click(function() {
    $(this).toggleClass('down').siblings().removeClass('down');
  });

Demo

2 Comments

is there a way to remove the hover feature from a button that has .addClass('down') to it?
It'd be better to do that using CSS, this question here should be able to help
1

You need to set the state of the other button at the same time?

$(document).ready(function () {
    $('a#gridBtn').click(function () {
        $(this).toggleClass("down");
        $('a#listBtn').attr('class', 'up');
    });
    $('a#listBtn').click(function () {
        $(this).toggleClass("down");
        $('a#gridBtn').attr('class', 'up');
    });
});

2 Comments

This worked great with what i have. Is there a way to have a#gridBtn toggleClass down by default on page load because that is how it is displayed
<a id="gridBtn" title="View as grid" class="btn down" href="#"> this will be changed by jquery when the click event happens

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.