Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

eslam said
PLUS
eslam said
Courses Plus Student 6,734 Points

How to change div content onclick

i don't think that innerHTML is the right thing to do here any suggestion ? i want to actually replace the div with another div

2 Answers

Sam Gord
Sam Gord
14,084 Points

i think your other option is actually a better practice than mine ;) and i don't think using a plugin is necessary for the demanded transition . u can achieve it with a mixture of css and javascript by adding a class at a certain point and removing it at another certain point . but u can use plugins to .

happy coding .

Sam Gord
Sam Gord
14,084 Points

Hi, this is one way of doing it --- >

for example i have a div with the id of one

<div id="one">1</div>

and i want to replace this div with another div when this div is clicked . and im going to give that other div an id of two just for now.then im going to give these divs (one of em still doesn't exist though) some sample styles just to see what happens.

#one{
  width: 100px;
  height: 100px;
  border: 1px solid red;
}
#two{
  width: 100px;
  height: 100px;
  border: 1px solid blue;
}

and then here's the good js part -->

// get the element
var div1 = document.getElementById('one');

// set an event listener for it
div1.addEventListener('click',function(){
//   create a new div
  var div2 =  document.createElement('div');
//   giv that div an id of two
  div2.setAttribute("id", "two");
//   append the new div to the dom
  this.parentNode.appendChild(div2);
//   remove the clicked div
  this.parentNode.removeChild(this);
});

as you can see, first i select the div i want to interact with ( give onclick event to ) . then im going to give it the event listener , and when it clicks tell it to : 1st - create a new div element. 2nd - give that new element an id of two , so when it appears i can see it did 3rd - appending that new div to the parent ( which is the body element here in my example but can be any element depending on the html markup, and 4th - removing the clicked div so it looks like it replaced completely with a new div .

happy coding ;)

eslam said
eslam said
Courses Plus Student 6,734 Points

Thank you so much for your answer, another option : can i stack divs on top of each other and change the display ? will it be a bad practice ? like onclick i will set div1 display to none and div2 display to block,

anyway how to make a smooth transition between both ? do i need to use a plugin or something