0
$(".normalText").css("border", "0px solid green");

How would you replace this jQuery statement with a plain JavaScript equivalent?

3
  • i didn't get you, can you please elaborate some more Commented Apr 5, 2011 at 11:19
  • document.getElementsByClassName( className ); for modern browsers only, otherwise there's no javascript replacement to get elements by className. Commented Apr 5, 2011 at 11:22
  • 1
    @Mithun P: It sounds like he wants vanilla JavaScript functions to replace the jQuery ones (I know, right?!) Commented Apr 5, 2011 at 11:23

4 Answers 4

2

To get a NodeList containing all elements with class="normalText", you could use:

var els = document.getElementsByClassName('normalText');

And then loop through them:

var els = document.getElementsByClassName('normalText'),
    len = els.length;
while (len--) {
  els[len].style.border = '3px solid hotpink';
}

getElementsByClassName isn’t supported in some older browsers. If support is an issue, you could use a fallback like this or this.

On the other hand, if you’re only supporting modern browsers, you might as well use querySelectorAll and Array#forEach:

[].forEach.call(document.querySelectorAll('.normalText'), function(el) {
  el.style.border = '3px solid hotpink';
});
Sign up to request clarification or add additional context in comments.

Comments

1

For modern browsers you can use the .getElementsByClassName() method MDC docs

document.getElementsByClassName('normalText');

for more modern you can use the .querySelectorAll() method MDC docs

document.querySelectorAll('.normalText');

for older browsers you can do (taken from http://robertnyman.com/2005/11/07/the-ultimate-getelementsbyclassname/)

function getElementsByClassName(oElm, strTagName, strClassName){
    var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
    var arrReturnElements = new Array();
    strClassName = strClassName.replace(/\-/g, "\\-");
    var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
    var oElement;
    for(var i=0; i<arrElements.length; i++){
        oElement = arrElements[i];
        if(oRegExp.test(oElement.className)){
            arrReturnElements.push(oElement);
        }
    }
    return (arrReturnElements)
}

and call it with

getElementsByClassName(document, "*", "normalText");

2 Comments

document.querySelector will only return the first matched element.
@Mathias, so true.. had the querySelectorAll in mind ... thanks for catching it
0

Try

document.getElementsByClassName('normalText').style.border = '0px solid green';

Comments

0

There's nothing like this built-in into Javascript as far as I know (edit: actually there's getElementsByClassName which works in some browsers only).

I guess your best chance is to write a function which loops through each DOM element and compares the class field with its argument.

Comments

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.