Solved

how do you toggle on off buttons

Posted on 2014-01-23
4
227 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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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…

777 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