1

Possible Duplicate:
Changing a CSS rule-set from Javascript

I am creating a list of elements in my jsp page with the same class name. Say I am creating 4 elements with same class name as "someName". Now, I want to modify the style of these elements dynamically using javascript.i.e, when a style is changed for the class it should reflect in all the elements referenced by the class name.Also, I have to do this with IE8. Can this be accomplished?

Thanks

3
  • Problem statement is unclear. Can you please post code snippet? Commented Oct 18, 2012 at 15:14
  • check this out, it's about changing classes on elements stackoverflow.com/questions/3452778/jquery-change-class-name Commented Oct 18, 2012 at 15:18
  • I can't use jQuery. Another big limitation is I have to achieve this using IE8, that too by using plain old javascript. Commented Oct 21, 2012 at 6:22

3 Answers 3

1

Short answer: YES

Ah you also want to know how? Why haven't you asked for the how?

I give you an example in jQuery

$(".someName").css("background-color","green:);

That would change the background-color all elements with the class 'someName'

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

1 Comment

Thanks for your reply Sven. But, i have to use simple pure javascript without any jQuery thing. That too i have to make it IE8 compatible.
1

The best way to do this is to add an additional class to the relevant elements.

Javascript

var elements =  document.getElementsByClassName('oldClass');
for(var i = 0;i<elements.length;i++){
    elements[i].className += ' newClass' //dont forget the first space
}

CSS

oldClass{color:red;}
newClass{color:white; font-weight:bold;}

the jQuery solution is more elegant, but you can do it with plain ol' javascript too.

1 Comment

getElementsByClassName() doesn't work with IE8. isn't it?
-1

In general, I would do as @sven-bieder suggests, but to answer your specific question: check out this answer https://stackoverflow.com/a/1409250/45948

5 Comments

You referred the op to an internal link, which in turn refers to an external link....why???
The internal link is to see the SO question with the according answers. What's the problem with that? Is it the extra click that bothers you?
Its the fact that your answer adds no value. -1
I pointed him to a valid answer, so what's your problem? And you're the one to decide if it's added value. Did you know you can change rules or do you know how to do it? I think it is added value.
Just quit whining. I'm not going to argue over a downvote.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.