CoderZone.org

Category: >> JavaScript >> Add and Remove classes in Javascript Bookmark and Share

<< lastnext >>

Snippet Name: Add and Remove classes in Javascript

Description: Example of how to dynamically add and remove a class from an element using Javascript.

Also see:
» Add a hidden field to a form dynam...
» Add more fields to a form dynamica...
» Remove empty lines from a string o...
» Easy way to break apart an email a...
» Remove empty lines from text files
» Check for a Valid Email Address (V...
» Get every 2nd (or 3rd, 4th, ...) e...
» Grab the last element of an array
» Remove Duplicate Lines from a File
» Check For A Valid Email Address
» Get the computed style of an eleme...
» Find All Elements By Class
» Dynamically create HTML elements
» Add PSOUG Search to SQL Developer
» Dynamically add and remove elements
» Search array elements for a substr...
» Add a period at the end of a strin...
» Remove Extra Spaces From A String
» Dynamically Add/Remove rows in HTM...
» Unset unkown array element
» FLASHBACK: Syntax Elements
» Remove Duplicate Lines from a File
» Check email address
» Date Functions: ADD_MONTHS
» Date Functions: Add and Subtract f...
» Add dashes to credit card numbers
» Get last added insert ID
» Add and Subtract dates
» Remove all non-ASCII chars

Comment: (none)

Author:
Language: JAVASCRIPT
Highlight Mode: JAVASCRIPT
Last Modified: January 09th, 2011

 
<script type="text/javascript">
    function addClass(){
        document.getElementById("myElement").className += " myClass";
    }
 
    function removeClass(){
        document.getElementById("myElement").className = document.getElementById("myElement").className.replace(/\bmyClass\b/,'')
    }
</script>
 
 
 
 
<button onclick="addClass()">Add Class</button>
<br />
<button onclick="removeClass()">Remove Class</button>
 
There haven't been any comments added for this snippet yet. You may add one if you like.  Add a comment 
© coderzone.org | users online: 12