0

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 {
        }

    }
3
  • Your i variable is global which is a very bad thing (imagine calling another function which also loops over i inside that loop). Use for(var i = 0; ....) instead Commented Apr 10, 2012 at 10:35
  • Why not give the desired div an id or name so you can reference it directly...? Commented Apr 10, 2012 at 10:35
  • Name? Bad idea. ID? Good idea. Commented Apr 10, 2012 at 10:35

1 Answer 1

5

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

Use == instead of =:

if(element[i].id = 'right') {
                 ^
            -----|
Sign up to request clarification or add additional context in comments.

1 Comment

I knew it would be something simple. Thanks!

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.