Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Changing CSS Class on Click (CSS + Javascript)

Changing CSS Class on Click (CSS + Javascript)
Thread Tools
Mac Elite
Join Date: Sep 2000
Location: 'round the corner
Status: Offline
Reply With Quote
Dec 3, 2004, 10:07 PM
 
So, i am trying to make a link that's class will change when someone clicks on it. I do not know Javascript, but I do know CSS, and so far I have this line of code:

Code:
<a href="#" onclick="repalceImage(); return false;" rel="thephoto" class="front"> <span>this is where the photo goes</span></a>
with this script, and the results are none:

Code:
function replaceImage() { var foto = document.getElementsBySelector('a[rel="thephoto"]') for (i=0;i<foto.length;i++) if ( foto.className == "front";) { switchTo("back"); } else if ( foto.className == "back";) { switchTo("front"); } } return null; } function switchTo(side){foto.className = side;}
Could someone give me some help, please?
     
Grizzled Veteran
Join Date: Nov 2003
Location: Hebburn, UK
Status: Offline
Reply With Quote
Dec 4, 2004, 07:12 AM
 
document.getElementsBySelector isn't actually a DOM method, but something that Simon Willison came up with a year or so ago. To use it, you'll need to include the js that makes it work.

For what you're doing though, it doesn't seem necessary to use it, unless you want to change a host of elements with the same class? If you're just changing one element (the one clicked) then this should work:

Code:
function replaceImage(el) { el.className = (el.className == "front") ? "back" : "front"; }
with the anchor looking like:

Code:
<a href="#" onclick="replaceImage(this); return false;" rel="thephoto" class="front"> <span>this is where the photo goes</span></a>
I've left in the rel attribute and the inner span because you had them...
Just who are Britain? What do they? Who is them? And why?

Formerly Black Book
     
Clinically Insane
Join Date: Nov 1999
Status: Offline
Reply With Quote
Dec 4, 2004, 07:48 AM
 
BlackBook is correct; getElementsBySelector() is not a standard in any browser, but a nifty little JavaScript routine that can help you with your own JavaScript stuff. To use it, though, you'll need to include his JavaScript alongside your own.

This said, there's another wrinkle to watch out for: it's possible to have more than one class on an element (all browsers, even IE/Windows, support this). It's highly useful for some things, but if you ever go that route then the technique of just replacing an element's className will become very dangerous. This is a serious problem with the DOM.

This page at Dean Edwards' site shows another technique which allows you to switch only the class you want, and leave alone any other classes which might be on the element you're switching. You don't have to use his IE7 JavaScript to make it work (though you may want to; as it's nothing short of godlike in what it does).

If you're interested in getElementsBySelector, by the way, then you may want to check out Dean Edwards' cssQuery engine. It seems to work in all browsers tested so far, and it's more powerful than getElementsBySelector, even though it works on the same principle.
You are in Soviet Russia. It is dark. Grue is likely to be eaten by YOU!
     
Mac Elite
Join Date: Sep 2000
Location: 'round the corner
Status: Offline
Reply With Quote
Dec 4, 2004, 03:45 PM
 
Thanks both, here is the finished product you helped me with:

Distinct Photos

(hint- click on the photos)
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 09:23 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2