mkyong
mkyong

Reputation: 12947

Change Color of Div Dynamically - Javascript

I think this should be a simple question, but I'm having trouble figuring it out. I have two divs within a parent div and I want to change the color of just the right div (javascript). My code loops through the child divs and then changes the color of both. I can't figure out how to select just the right div and change its color. Here is my code (this changes the color of both):

    var color = "#fdd42e";
    var element = document.getElementById(user_id).childNodes;

    for (i=0;i< element.length;i++) {

        if(element[i].id = 'right') {
            element[i].style.background = color;
        }
        else {
        }

    }

Upvotes: 0

Views: 3167

Answers (1)

Sarfraz
Sarfraz

Reputation: 382881

I can't figure out how to select just the right div and change its color.

Use == instead of =:

if(element[i].id = 'right') {
                 ^
            -----|

Upvotes: 5

Related Questions