2

Could anyone give me a hand with the following JavaScript issue.

I am trying to come up with a function that adds a class to a div that has a specified class.

I have tried to come up with something based on what a few people have said but it doesn't seem to work.

http://jsfiddle.net/samsungbrown/vZ9Hu/

Where am I going wrong?

Thanks

function toggleClass(matchClass,content) {
    var elems = document.getElementsByTagName('*'),i;
    for (i in elems) {
        if((" "+elems[i].className+" ").indexOf(" "+matchClass+" ") > -1) {
            elems[i].classList.toggle(content);
        }
    }
}

window.onload = function () {
    toggleClass("col-left","display");
}
2
  • can you use jquery? because that would be a really simple $(matchClass).addClass(otherClass) Commented Aug 16, 2013 at 9:49
  • window.onload is not working Commented Aug 16, 2013 at 9:51

1 Answer 1

1

Because of some quirks in jsFiddle your code doesn't run. Remove the .onload wrapper and your code runs. See: http://jsfiddle.net/vZ9Hu/1/

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

1 Comment

Or just change the jsFiddle option from "onLoad" to "No wrap".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.