Question

Selectable images using Javascript/CSS (swap classes onclick)

Asked by: xill2678

I have groups of images that I'd like people to be able to select.

Image Group One:
There's four images, 3 are assigned the css class "select-off", one is assigned the css class "select-on". I have corresponding CSS written, but that's not relevant to what I'm trying to do right now. What I want is when someone clicks on one of the images, the css class is swapped. This will in turn swap the other classes for the other images as well. So only one image from the group can be selected at a time. This works exactly like the radio button of an html form.

Image Group Two:
there's four images, 3 are assigned the css class "select-off", one is assigned the css class "select-on", just as the group described above. The difference with this group, is that multiple images can be selected within the same group. Clicking the image will swap the class from the current class to the opposite. Clicking again will swap again. These work exactly like selecting/unselecting checkboxes in an html form.

Since all of the images in the different groups are different from each other, I need to be able to swap classes, not images. Also, I have many different instances of both Group One and Group Two on the same page.

Ive tried adjusting many different javascripts I've found around the Net, but with no luck. I know this is very simple, and I've come close to what I want on my own, but my js skills just arent very good. Does anyone know of a script available that I can use for this without modifications? Or does anyone perhaps already have a script they care to share? Thanks :)

A final quick note, selecting an image should NOT refresh the page. Links that require href="#" in the tag to operate properly wont help me, as that would conflict with the way I have the page already setup. I need the link tag that wraps the image to not contain href at all. The images are being selected, but not actually linking to anything.

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2008-05-05 at 12:00:49ID23377344
Tags

Javascript, CSS

,

all

,

none

Topics

JavaScript

,

Dynamic HTML (DHTML)

,

Cascading Style Sheets (CSS)

Participating Experts
1
Points
500
Comments
12

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. ONCLICK : 'change selection / unselect' ?
    Hi, I have something that uses iframes. all is fine, but i have an 'aesthetic' issue with the links... selecting a link (outside iframe)changes the iframe ok, but leaves the link with a 'selection box/boundary' around it (the same as any link, only you usually dont see it ...
  2. Applying a CSS Rule with JS
    Hi, there is a line of JavaScript, which applies a CSS Rule "on click" or "on Rollover" to a specific item on the page, e.g. to a TD or to a font. As I can remember (it is very long time ago), it was something like "onClick= this.style ...", B...
  3. CSS/JS/CF toggle CSS based on query output
    I am posting this question under javascript because it relates more closely with JS/CSS than Coldfusion, however Coldfusion is the server script language I'm using and the result is based upon what is outputed from a query. basically want to add a bit more functionality to t...
  4. IE CSS/JS Problem
    Greetings, This page works as intended in Firefox 1.5, and for the most part in IE, the only problem being that when a tab is clicked, it does not take on the 'tabActive' CSS class. Can anyone point out what I'm missing? <html> <head> <style> #tabs a, #ta...
  5. CSS/JS - Toggle Fieldset Border
    I am lookign for a way - within JS/CSS to toggle the border from 0px to 1 px - with the click of a checkbox, for a fieldset - is this possible and if so how?

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: cwolvesPosted on 2008-05-05 at 15:12:35ID: 21503443

done with spans because it's easier for now, just convert to images:

<style>
span{
      border:1px solid blue;
}
.class1{
      background-color: yellow;
}
.class2{
      background-color: gray;
}
</style>
<div instanceClass="class1" generalClass="class2">
      <span id="initial">test1</span>
      <span>test2</span>
      <span>test3</span>
      <span>test4</span>
</div>
<script>
function selectItem(ev){
      ev = ev || window.event;
      var item = ev.target || ev.srcElement;

      var par  = item.parentNode;
      var iCls = par.getAttribute('instanceClass');
      var gCls = par.getAttribute('generalClass');

      var child = par.getElementsByTagName('SPAN');
      for(var i=0; i<child.length; i++){
            child[i].className = child[i]==item?iCls:gCls;
      }
}

function prepItems(items){
      for(var i=0; i<items.length; i++){
            items[i].onclick = selectItem;
      }
}

prepItems(document.getElementsByTagName('SPAN'));
</script>

 

by: cwolvesPosted on 2008-05-05 at 15:14:09ID: 21503457

as for group #2:


<img onclick="this.className=(this.className=='class1'?'class2':'class1')">
...repeat

 

by: xill2678Posted on 2008-05-05 at 16:42:09ID: 21503853

Thank you cwolves. This is a great help! i'm having a bit of trouble with it though. I changed the spans to images, and added the code in your second post to the "checkbox-style" images, but they're acting the same as the "radio-style" images. Also, if any other image is clicked in the page, it clears the selection of all image groups. I've attached the code snippet I'm using (i've used the same image as placeholders here, but they will actually all be different images, but same sizes):

<style>
#buttons img {display:block; float:left; margin:0 3px 0 0; border:2px solid #FFFFFF; cursor:pointer; width:30px; height:30px;}
#buttons img.class1 {border:2px solid #0066FF; width:30px; height:30px; cursor:pointer;}
#buttons img.class2 {border:2px solid #FFFFFF; width:30px; height:30px; cursor:pointer;}
</style>
</head>
<body>
 
RADIO-STYLE 1<br>
<div instanceClass="class1" generalClass="class2" id="buttons">
      <img src="images/customize-repeat-across.gif" width="30" height="30" class="class1">
      <img src="images/customize-repeat-across.gif" width="30" height="30">
      <img src="images/customize-repeat-across.gif" width="30" height="30">
	  <img src="images/customize-repeat-across.gif" width="30" height="30">
</div>
<br><br><br>
RADIO-STYLE 2<br>
<div instanceClass="class1" generalClass="class2" id="buttons">
      <img src="images/customize-repeat-across.gif" width="30" height="30" class="class1">
      <img src="images/customize-repeat-across.gif" width="30" height="30">
      <img src="images/customize-repeat-across.gif" width="30" height="30">
	  <img src="images/customize-repeat-across.gif" width="30" height="30">
</div>
<br><br><br>
CHECKBOX STYLE 1<br>
<div instanceClass="class1" generalClass="class2" id="buttons">
	 <img src="images/customize-repeat-across.gif" width="30" height="30" onclick="this.className=(this.className=='class1'?'class2':'class1')">
	 <img src="images/customize-repeat-across.gif" width="30" height="30" onclick="this.className=(this.className=='class1'?'class2':'class1')" class="class1">
	 <img src="images/customize-repeat-across.gif" width="30" height="30" onclick="this.className=(this.className=='class1'?'class2':'class1')">
	 <img src="images/customize-repeat-across.gif" width="30" height="30" onclick="this.className=(this.className=='class1'?'class2':'class1')">
</div>
<br><br><br>
CHECKBOX STYLE 2<br>
<div instanceClass="class1" generalClass="class2" id="buttons">
	 <img src="images/customize-repeat-across.gif" width="30" height="30" onclick="this.className=(this.className=='class1'?'class2':'class1')">
	 <img src="images/customize-repeat-across.gif" width="30" height="30" onclick="this.className=(this.className=='class1'?'class2':'class1')" class="class1">
	 <img src="images/customize-repeat-across.gif" width="30" height="30" onclick="this.className=(this.className=='class1'?'class2':'class1')">
	 <img src="images/customize-repeat-across.gif" width="30" height="30" onclick="this.className=(this.className=='class1'?'class2':'class1')">
</div>
<br><br><br>
REGULAR IMAGES<br>
<img src="images/customize-repeat-across.gif" width="30" height="30">
<img src="images/customize-repeat-across.gif" width="30" height="30">
<img src="images/customize-repeat-across.gif" width="30" height="30">
<img src="images/customize-repeat-across.gif" width="30" height="30">
<br><br><br>
<script>
function selectItem(ev){
      ev = ev || window.event;
      var item = ev.target || ev.srcElement;
      var par  = item.parentNode;
      var iCls = par.getAttribute('instanceClass');
      var gCls = par.getAttribute('generalClass');
      var child = par.getElementsByTagName('img');
      for(var i=0; i<child.length; i++){
            child[i].className = child[i]==item?iCls:gCls;
      }
}
 
function prepItems(items){
      for(var i=0; i<items.length; i++){
            items[i].onclick = selectItem;
      }
}
prepItems(document.getElementsByTagName('img'));
</script>
 
</body>

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:

Select allOpen in new window

 

by: cwolvesPosted on 2008-05-05 at 16:58:00ID: 21503904

the problem is with the prepItems function call which is basically preparing ALL images on the page for this.

Try calling it once on each div that you want to function like this:

prepItems(document.getElementById('parentDiv1').getElementsByTagName('img'));

where 'parentDiv1' is the ID of a 'radio-style' div.  You need to change the id's from 'buttons' to 'buttons1', 'buttons2', etc.

 

by: xill2678Posted on 2008-05-05 at 17:26:49ID: 21503996

thanks a million cwolves, it works perfectly now in all browsers. I'm still having trouble with the checkbox-style images though. they act the same as the radio-style buttons instead of allowing toggling back and forth regardless of what other images are selected.

I put the code you gave in the img tag like this:

<img src="images/customize-repeat-across.gif" width="30" height="30" onclick="this.className=(this.className=='class1'?'class2':'class1')">

is this not correct? I noticed the "?" in the code snippet, never seen that before so I'm not sure if that's suppose to be a ? or if its a character that my pc isnt recognizing.....

 

by: cwolvesPosted on 2008-05-05 at 17:54:17ID: 21504088

no, it's correct.  Are you calling prepItems on the "checkbox" div?  If so, you shouldn't be.

 

by: xill2678Posted on 2008-05-05 at 17:56:58ID: 21504097

no i call it from the bottom of the js. attached is my entire code

<style>
#buttons1 img {display:block; float:left; margin:0 3px 0 0; border:2px solid #FFFFFF; cursor:pointer; width:30px; height:30px; opacity:0.15;filter:alpha(opacity=15);}
#buttons1 img.class1 {border:2px solid #0066FF; width:30px; height:30px; cursor:pointer; opacity:1.0;filter:alpha(opacity=100);}
#buttons1 img.class2 {border:2px solid #FFFFFF; width:30px; height:30px; cursor:pointer; opacity:0.15;filter:alpha(opacity=15);}
 
#buttons2 img {display:block; float:left; margin:0 3px 0 0; border:2px solid #FFFFFF; cursor:pointer; width:30px; height:30px; opacity:0.15;filter:alpha(opacity=15);}
#buttons2 img.class1 {border:2px solid #0066FF; width:30px; height:30px; cursor:pointer; opacity:1.0;filter:alpha(opacity=100);}
#buttons2 img.class2 {border:2px solid #FFFFFF; width:30px; height:30px; cursor:pointer; opacity:0.15;filter:alpha(opacity=15);}
 
#buttons3 img {display:block; float:left; margin:0 3px 0 0; border:2px solid #FFFFFF; cursor:pointer; width:30px; height:30px; opacity:0.15;filter:alpha(opacity=15);}
#buttons3 img.class1 {border:2px solid #0066FF; width:30px; height:30px; cursor:pointer; opacity:1.0;filter:alpha(opacity=100);}
#buttons3 img.class2 {border:2px solid #FFFFFF; width:30px; height:30px; cursor:pointer; opacity:0.15;filter:alpha(opacity=15);}
 
#buttons4 img {display:block; float:left; margin:0 3px 0 0; border:2px solid #FFFFFF; cursor:pointer; width:30px; height:30px; opacity:0.15;filter:alpha(opacity=15);}
#buttons4 img.class1 {border:2px solid #0066FF; width:30px; height:30px; cursor:pointer; opacity:1.0;filter:alpha(opacity=100);}
#buttons4 img.class2 {border:2px solid #FFFFFF; width:30px; height:30px; cursor:pointer; opacity:0.15;filter:alpha(opacity=15);}
</style>
</head>
 
<body>
 
RADIO-STYLE 1<br>
<div instanceClass="class1" generalClass="class2" id="buttons1">
      <img src="images/customize-repeat-across.gif" width="30" height="30" class="class1">
      <img src="images/customize-repeat-across.gif" width="30" height="30">
      <img src="images/customize-repeat-across.gif" width="30" height="30">
	  <img src="images/customize-repeat-across.gif" width="30" height="30">
</div>
<br><br><br>
RADIO-STYLE 2<br>
<div instanceClass="class1" generalClass="class2" id="buttons2">
      <img src="images/customize-repeat-across.gif" width="30" height="30" class="class1">
      <img src="images/customize-repeat-across.gif" width="30" height="30">
      <img src="images/customize-repeat-across.gif" width="30" height="30">
	  <img src="images/customize-repeat-across.gif" width="30" height="30">
</div>
<br><br><br>
CHECKBOX STYLE 1<br>
<div instanceClass="class1" generalClass="class2" id="buttons3">
	 <img src="images/customize-repeat-across.gif" width="30" height="30" onclick="this.className=(this.className=='class1'?'class2':'class1')">
	 <img src="images/customize-repeat-across.gif" width="30" height="30" onclick="this.className=(this.className=='class1'?'class2':'class1')" class="class1">
	 <img src="images/customize-repeat-across.gif" width="30" height="30" onclick="this.className=(this.className=='class1'?'class2':'class1')">
	 <img src="images/customize-repeat-across.gif" width="30" height="30" onclick="this.className=(this.className=='class1'?'class2':'class1')">
</div>
<br><br><br>
CHECKBOX STYLE 2<br>
<div instanceClass="class1" generalClass="class2" id="buttons4">
	 <img src="images/customize-repeat-across.gif" width="30" height="30" onclick="this.className=(this.className=='class1'?'class2':'class1')">
	 <img src="images/customize-repeat-across.gif" width="30" height="30" onclick="this.className=(this.className=='class1'?'class2':'class1')" class="class1">
	 <img src="images/customize-repeat-across.gif" width="30" height="30" onclick="this.className=(this.className=='class1'?'class2':'class1')">
	 <img src="images/customize-repeat-across.gif" width="30" height="30" onclick="this.className=(this.className=='class1'?'class2':'class1')">
</div>
<br><br><br>
REGULAR IMAGES<br>
<img src="images/customize-repeat-across.gif" width="30" height="30">
<img src="images/customize-repeat-across.gif" width="30" height="30">
<img src="images/customize-repeat-across.gif" width="30" height="30">
<img src="images/customize-repeat-across.gif" width="30" height="30">
<br><br><br>
<script>
function selectItem(ev){
      ev = ev || window.event;
      var item = ev.target || ev.srcElement;
      var par  = item.parentNode;
      var iCls = par.getAttribute('instanceClass');
      var gCls = par.getAttribute('generalClass');
      var child = par.getElementsByTagName('img');
      for(var i=0; i<child.length; i++){
            child[i].className = child[i]==item?iCls:gCls;
      }
}
 
function prepItems(items){
      for(var i=0; i<items.length; i++){
            items[i].onclick = selectItem;
      }
}
prepItems(document.getElementById('buttons1').getElementsByTagName('img'));
prepItems(document.getElementById('buttons2').getElementsByTagName('img'));
prepItems(document.getElementById('buttons3').getElementsByTagName('img'));
prepItems(document.getElementById('buttons4').getElementsByTagName('img'));
</script>
 
</body>
</html>

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:

Select allOpen in new window

 

by: cwolvesPosted on 2008-05-05 at 18:01:39ID: 21504111

I'm saying do -not- call it on 3 & 4.

 

by: xill2678Posted on 2008-05-05 at 18:25:00ID: 21504191

oh I see! I thought you were asking if i had added the snippets directly inside the div tags. my mistake. it's working beautifully now! thanks alot! you cured my three-day headache :)

 

by: cwolvesPosted on 2008-05-05 at 18:25:37ID: 21504192

:)

 

by: xill2678Posted on 2008-05-05 at 18:27:09ID: 31455192

thanks again :)

 

by: xill2678Posted on 2008-05-05 at 20:48:27ID: 21504633

I've run into another problem and posted it as a different question with new points. i thought you might want to take a look since you were the one to help with the first part of this ;)

The new question is located here:
http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_23378462.html

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...