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
Solved

how do you toggle on off buttons

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

860 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