Making a button toggle a div's class

I'm making some basic site with Twitter Bootstrap

I have one div and two classes I want to use. I want to implement button which will change div's class and then after clicking again, to change it back.

Found many ways, but none of them changes class back. The Best way would be to use Bootstrap JS or Jquery

Thanks

Answers


Demo 1: http://jsfiddle.net/Jsqh4/ Demo 2 (separate button): http://jsfiddle.net/Jsqh4/1/

Bootstrap uses jQuery for its components, so a jQuery solution is probably easiest for you.

<style>
    .style1{ background-color: yellow; }
    .style2{ background-color: green; }‚Äč
</style>

<div id="div1" class="style1">
    Here is a div.
</div>

<input type="button" id="btn1" value="Click Me" />

<script>
    $("#btn1").click(function(){
        $("#div1").toggleClass("style2");
    });
</script>

See also: http://api.jquery.com/toggleClass/


Need Your Help

Making results returned by AJAX call live for sorting

jquery

I have an initial page (program_finder.asp) that makes an AJAX call for some results:

What is the best way to iterate through a strongly-typed generic List<T>?

c# .net vb.net generics collections

What is the best way to iterate through a strongly-typed generic List in C#.NET and VB.NET?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.