Child Div styles in javascript

General Tech Learning Aids/Tools 2 years ago

0 2 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Learning Aids/Tools related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

 

I have got a preview box that gets the color and then changes it when the user does. However i have a child div that needs changine. how can i select the child div through get element by id? this is the java script i have at the moment, how do i change this so i can get the child div front to change color

document.getElementById("flip3D").style.backgroundColor=p1;
#flip3D{
    width:200px;
    height:200px;
    margin:20px;
    float:left;
}
#flip3D > .front{
    ... style stuff is all there just to much toput in and worry about
}
profilepic.png
manpreet 2 years ago

You mean something like this?

var front = document.querySelectorAll('#flip3D .front');
for(var i=0; i<front.length; i++){
  front[i].style.backgroundColor = 'red';
}

or like:

var flipEl = document.getElementById("flip3D");
var frontEl = flipEl.getElementsByClassName('front');
for(var i=0; i<frontEl.length; i++){
    frontEl[i].style.backgroundColor = p1;
}

Why not do it using CSS? (Your Question is pretty unclear so my hands are tied)


0 views   0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.