1

I am trying to make a script that sets a class for the label of a checkbox when i click it once and when i click it again it reverts to the first class.

This is the code i have:

    <label for="img1">
    <img class="img1" src="Images/testimg.jpg" onclick="javascript:test()" id="t1" />
    </label>

    <input type="checkbox" class="chk " id="img1" name="img1" value="1" />

I want the test function to assign the class img2 when it is called and when i call it again to assign the class img1.

3
  • Do you need to replace current classes (a trivial task) or add a new one (not difficult, but requires a helper function)? Commented May 8, 2013 at 14:56
  • 2
    For your entertainment, a Stack Overflow classic: Not enough jQuery. Commented May 8, 2013 at 14:58
  • Perhaps you can use this: document.getElementById("MyElement").classList.toggle('MyClass'); Commented Feb 22, 2018 at 12:41

4 Answers 4

4

JavaScript

function toggleClass(element, origin, target) {
    var newClass = element.className.split(" ");
    for (var i = 0, i < newClass.length; i++) {
        if (origin.localeCompare(newClass[i]) == 0) {
            newClass[i] = target;
        };
    };
    element.className = newClass.join(" ");
};
// Usage
var img = document.getElementById("img1");
toggleClass(img, "img1 img2");

jQuery

$("#img1").toggleClass("img1 img2");

More here.

Sign up to request clarification or add additional context in comments.

3 Comments

he said javascript, not jquery
Don't you mean .toggleClass("img1 img2");?
@RocketHazmat Good catch.. @alex23: update your question accordingly: api.jquery.com/toggleClass/#toggleClass-className One or more class names (separated by spaces)...
2
function test() {
    t1.className = t1.className == 'img1' ? 'img2' : 'img1';
}

Related: Change an element's class with JavaScript

6 Comments

This is ok I suppose, t1.className = t1.className == t1.className.indexOf('img1') > -1 ? t1.className.replace('img1','img2') : t1.className.replace('img2','img1')';
that also won't work. if you had 'img10' as a classname then you'd end up with 'img20' after the swap, which isn't what you want.
I would say duplicate rather than related.
@alex23, unless specified differently, assumed single classed dom element. I'm personnally pulling the chariot because I want to, not only because of the carrot.
@XaviLópez, right, depends on your morning mood :D
|
0

If you are not using jQuery I will suggest you 2 things:

  1. With onclick property, you don't need to set javascript: before calling a function. It will not work if you use it.
  2. You can send the element through the function like this: onclick=test(this). Now you will only have to compare his class.
function testFunction(element) {
    if (element.className === 'img1') {
        element.className = 'img2';
    } else {
        element.className = 'img1';
    }
}

TAKE CARE if you use multiclasses, because you will need to add them into the classname. This function will remove full class name for the new one.

Comments

-1

You could use JQuery's toggleClass method:

function toggler() {
    $('#t1').toggleClass('img2 img1')

}

1 Comment

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.