Solved

how do you toggle on off buttons

Posted on 2014-01-23
4
225 Views
Last Modified: 2014-01-23
Hi,

I have a table that has some images and text in it. I want to be able to have an "on" off" state for the images so the user knows what was clicked on.

It starts with one of the table cells having the on state and the rest having the off state.

I don't know how to make the toggle function so the other images "knows" which one is on and the rest are off

Here is my table:

<table cellspacing="7" cellpadding="0" border="0">
	<tr>
	    <td width="60" align="left">View:</td>
	    <td width="100"align="left"><a><img src="images/white_on.png" />All Types</a></td>
	    <td width="113" align="left"><a><img src="images/orange_off.png" />On Demand</a></td>
		<td width="101" align="left"><a><img src="images/green_off.png" />Webinars</a></td>
		<td width="141" align="left"><a><img src="blue_off.png" />Seminars</a></td>
        </tr>
        </table>

Open in new window


Thanks!
0
Comment
Question by:smfmetro10
  • 2
  • 2
4 Comments
 
LVL 58

Expert Comment

by:Gary
ID: 39803968
Using jquery, I just added a background color to the image to show the state
http://jsfiddle.net/GaryC123/vTf2L/2/
<table cellspacing="7" cellpadding="0" border="0">
	<tr>
	    <td width="60" align="left">View:</td>
        <td width="100"align="left"><a><span class="off"></span>All Types</a></td>
	    <td width="113" align="left"><a><span class="off"></span>On Demand</a></td>
		<td width="101" align="left"><a><span class="off"></span>Webinars</a></td>
		<td width="141" align="left"><a><span class="off"></span>Seminars</a></td>
        </tr>
        </table> 
 

Open in new window

table a span {
    display:inline-block;
    height:20px;
    width:20px;
    border:1px solid blue
}
table a span.off {
    background:url("images/orange_off.png") red
}
table a span.on {
    background:url("images/orange_on.png") green
}

Open in new window

$(function() {
$("table a span").click(function(){
  $(this).toggleClass("on")
})
$("table a span:eq(0)").toggleClass("on")})

Open in new window

0
 

Author Comment

by:smfmetro10
ID: 39804048
Thanks for the reply!
The thing is that when you click on one of them, the other images have to go back to their off state.
0
 
LVL 58

Accepted Solution

by:
Gary earned 500 total points
ID: 39804059
http://jsfiddle.net/GaryC123/vTf2L/3/

$("table a span").click(function(){
    $("table a span").removeClass("on")
  $(this).toggleClass("on")
})
$("table a span:eq(0)").toggleClass("on")

Open in new window

0
 

Author Closing Comment

by:smfmetro10
ID: 39804080
Outstanding! Thanks!
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now