Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

how do you toggle on off buttons

Posted on 2014-01-23
4
Medium Priority
?
236 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
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…

604 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