tags:

views:

64773

answers:

6

How can I change a CSS class of an HTML element in response to an onClick event using JavaScript?

A: 

This is easiest with a library like jQuery:

<input type="button" onClick="javascript:test_byid();" value="id='second'" />

<script>
function test_byid()
{
    $("#second").toggleClass("highlight");
}
</script>
Jon Galloway
What does the javascript: pseudo-protocol do in a script-event ... It seems totally stupid to tell the javascript-interpretator, that it should treat script in a script-event as script !-) Only use of the javascript: pseudo-protocol is where you instead would use an url !o]
roenving
In that context, it isn't the pseudo-protocol - it's a loop label ... only there is no loop for it TO label.
David Dorward
A: 

No offense, but it's unclever to change class on-the-fly as it forces the css-interpretator to recalculate the visual presentation of the entire webpage ...

-- the reason is that it is nearly impossible for the css-interpretator to know if any inheritance or cascading could be changed, so the short answer is:

Never ever change className on-the-fly !-)

But usually you'll only need to change a property or two, and that is easily implemented:

function highlight(elm){
  elm.style.backgroundColor ="#345";
  elm.style.color = "#fff";
}
roenving
i've never experienced any performance issues with switching CSS classes myself. I think whatever performance hit there *might* be, it's far outweighed by the messiness of having styles/presentation mixed up in your javascript.
nickf
Hrm, obviously you never tested it ... In a realtime application consisting of thousands of rows nested with other elements I recognized a delay of several seconds, remaking it only to change properties it wasn't possible to recognize delay ...
roenving
Peter Boughton
If changing a className is causing noticeable performance problems, you have much bigger problems in the structure and design of your page/app. If not, shaving off a few milliseconds is not a good reason to pollute your application logic with styles.
eyelidlessness
this is the worst idea ever. changing classes is by far and away the easiest and cleanest way to update your CSS on the fly.
Jason
+1  A: 

You can use node.className like so:

  document.getElementById("blah").className = "cssclass";
Eric Wendelin
+46  A: 

To add a class to an element:

document.getElementById("MyElement").className += " MyClass";

To remove a class from an element:

document.getElementById("MyElement").className = document.getElementById("MyElement").className.replace(/\bMyClass\b/','')

To do that in an onclick event:

<script type="text/javascript">
    function changeClass()
    {
        // code examples from above
    }
</script>
...
<button onclick="changeClass()">My Button</button>


Better yet, use jQuery which allows you to do the following:

$j('#MyElement').addClass('MyClass');

$j('#MyElement').removeClass('MyClass');

$j('#MyElement').toggleClass('MyClass');

And also:

<script type="text/javascript">
    function changeClass()
    {
        // code examples from above
    }

    $j(':button:contains(My Button)').click(changeClass);
</script>
...
<button>My Button</button>


Peter Boughton
Excellent description! Thanks!
Helgi Hrafn Gunnarsson
A: 

this line: document.getElementById("MyElement").className = document.getElementById("MyElement").className.replace(/\bMyClass\b/','')

should be: document.getElementById("MyElement").className = document.getElementById("MyElement").className.replace('/\bMyClass\b/','');

Eric Bailey
A: 

@Eric Bailey You are right, there is an error there but your correction is not good. If you put the regex into quote, it will try to find the string instead of executing the regex. The right correction is:

document.getElementById("MyElement").className = document.getElementById("MyElement").className.replace(/\bMyClass\b/','')

should be:

document.getElementById("MyElement").className = document.getElementById("MyElement").className.replace(/\bMyClass\b/,'');

Bbp