EvanAgee.com
  • Who Am I?
  • Blog
  • Music
  • Devotionals
  • Social Networks
    • Facebook
    • Vimeo Videos
    • YouTube Videos
    • Flickr Photos
    • Dribbble
    • Pinterest
  • Resonate Creative
Home > Website Development > Prototype.js tip: Remove class name from a group of elements
Feb03 0

Prototype.js tip: Remove class name from a group of elements

Posted by Evan in Website Development

I’m using jQuery mostly these days but a few of my previous projects were built on Prototype so I’ve been keeping up with both libraries. I learned a cool trick today for Prototype.js that I thought I would share.

So, have you ever wanted to remove a certain class name from a group of tags? This might be useful if you have a list of options and you want to change the visual style of the element for the selected element while changing all other items back to their “default” state. This will do that!

Let’s say we have a few anchor tags that make up a row of tabs on the interface:

<a id="tab1" href="#" class="selected tabs" onclick="loadTab(1); return false;">Page 1</a>
<a id="tab2" href="# class="tabs" onclick="loadTab(2); return false;">Page 2</a>
<a id="tab3" href="# class="tabs" onclick="loadTab(3); return false;">Page 3</a>

When one of the <a> tags is clicked I want it to add a class name, and I want to remove the selected class from all other tags with a class name of “tabs”. I’ll assume that you’re familiar with the Element.addClassName() function of Prototype.js and skip that part, but I do want to show you an easy way of removing the “selected” class from all of the elements with a class of “tabs”.

function loadTab(NUM) {
      $$('.tabs').each( function (s) { s.removeClassName('selected'); });
      $('tab'+NUM).addClassName('selected');
      return true;
}

BOOM! Simple as that. Now you’ll be on your way to creating dynamic and beautiful UIs in no time!</a>

Comments:

Leave a Comment Cancel reply

You must be logged in to post a comment.

About Me

I'm a Christ-following website developer, filmmaker and songwriter. I love Jesus and love spending time and ministering to my brothers and sisters. I am owner/partner at Resonate Creative a creative services firm based in Fort Worth, TX and Systems Analyst at Gateway Church.

Facebook Activity

Twitter Updates

  • I just liked "DrupalCon Denver, OG7" on Vimeo: http://t.co/2DVRzStp 3 days ago
  • I liked a @YouTube video http://t.co/E8Qby7UI Baal T'shuvah (plus a story that will blow your mind!) 4 days ago
  • Selling my 17" MacBook Pro for $1850, contact me if interested! http://t.co/pjgIs9r2 6 days ago

Recent Posts

  • The lie of intellectualism
  • A quick update
  • He makes all things new: lessons from Job
  • I think you should know…
  • “The Train” by Evan Agee

Recent Comments

  • Josh Ward on Agee Design HQ, December 16, 2010
  • Josh Ward on New year update
  • Evan on Design as worship: It’s all about JESUS!
  • WholeApple on Design as worship: It’s all about JESUS!
  • Steve Jobs Says ‘Buckle Up’ for New Final Cut Studio | Mac Productions - Apple News and Tutorials on I got an email from Steve Jobs!!!!

© 2011 EvanAgee.com | Designed by Elegant Themes | Powered by WordPress