1
2
3
Lorem ipsum, dolor sit amet consectetur adipisicing elit. Commodi dolor blanditiis,
veritatis quisquam eveniet quo! Vero minima, incidunt culpa totam quis eveniet. Nostrum
alias exercitationem iste, ab eligendi deleniti fugit facere vel, quo maiores magni
nulla
dicta fuga error soluta autem unde dolorum ipsa quae. Velit totam earum veniam eveniet!
Notes
-
Click on any coloured
div
to toggle the addition/removal of thefloat-left
class
Adiv
with thefloat-left
class is labelledL
-
Click on the
button
to toggle the addition/removal of theclear-left
class
Adiv
with theclear-left
class are labelledC
- Click on
Div 1
for an interesting result -
Neither the
float-left
nor theclear-left
class are added to the lorem-ipsum-containingdiv
- Note that the
clear-left
class is not attached to thered div
-
The
yellow div
hasopacity: 0.5
-
(The
clear-left
class has been added to the subseqenth2
tag) - Relevant classes
.float-left { float: left } .clear-left { clear: left; }
Code
HTML
<div class="content"> <div> <div id="red_box"> <span>1</span> <span></span> </div> <div id="yellow_box"> <span>2</span> <span></span> </div> <div id="green_box"> <span>3</span> <span></span> </div> <div class="btn"> <button id="mybtn"> <span id="toggle">add</span> clear-left class to yellow and green divs </button> </div> <div> Lorem ipsum, dolor sit amet consectetur adipisicing elit. Commodi dolor blanditiis, veritatis quisquam eveniet quo! Vero minima, incidunt culpa totam quis eveniet. Nostrum alias exercitationem iste, ab eligendi deleniti fugit facere vel, quo maiores magni nulla dicta fuga error soluta autem unde dolorum ipsa quae. Velit totam earum veniam eveniet! </div> </div><!--content-->
#red_box, #green_box, #yellow_box { width: 10vw; height: 10vw; font-size: 2em; text-align: center; } #red_box { background-color: red; } #green_box { background-color: green; } #yellow_box { background-color: yellow; width: 20vw; opacity: 0.5; } .float-left { float: left } .clear-left { clear: left; } .example { clear: left; }
JavaScript
function toggleClass(id, toggleClass, toggleMsgOne, toggleMsgTwo) { if (document.getElementById(id)) { document.getElementById(id).addEventListener('click', function () { if (this.classList.contains(toggleClass)) { this.classList.remove(toggleClass); this.firstElementChild.innerHTML = toggleMsgTwo } else { this.classList.add(toggleClass); this.firstElementChild.innerHTML = toggleMsgOne; } }); } } toggleClass('red_box', 'float-left', "1L", "1"); toggleClass('yellow_box', 'float-left', "2L", "2"); toggleClass('green_box', 'float-left', "3L", "3"); document.getElementById('mybtn').addEventListener('click', function () { if (document.getElementById('toggle').innerHTML == "add") { document.getElementById('yellow_box').classList.add('clear-left'); document.getElementById('yellow_box').lastElementChild.innerText = "C"; document.getElementById('green_box').classList.add('clear-left'); document.getElementById('green_box').lastElementChild.innerText = "C"; this.innerHTML = "remove clear-left class from yellow and green divs"; } else { document.getElementById('yellow_box').classList.remove('clear-left') document.getElementById('yellow_box').lastElementChild.innerText = ""; document.getElementById('green_box').classList.remove('clear-left') document.getElementById('green_box').lastElementChild.innerText = ""; this.innerHTML = "add clear-left class to yellow and green divs"; } })