Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
let lamp = document.getElementById('lamp');
 for (let s = 0; s <100; s++) {
    let trafficLight = "";

       if (s % 3 == 0) {
          trafficLight = "red";
       }
       else if (s % 3 == 2) {
          trafficLight = "green";
        }
        else if (s % 1 == 0) {
          trafficLight = "yellow";
        }

 lamp.innerHTML += `<div class="traffic" id="traffic` + s + `">
                   <div class="lampu buled ${trafficLight}"></div>
                   <button class="button buled" data-index="`+ s + `"></button></div>`
 }
 function toggleLight(index) {
 let serliCantik = document.getElementById('traffic' + index).querySelector('.lampu');
 serliCantik.classList.toggle('on')
 }
 lamp.addEventListener('click', function (e) {
     if (e.target.classList.contains('button')) {
      let index = e.target.attributes['data-index'].value
      toogleLight(index)}
 });


What I have tried:

<pre>   let lamp = document.getElementById('lamp');
    for (let s = 0; s <100; s++) {
       let trafficLight = "";
    
          if (s % 3 == 0) {
             trafficLight = "red";
          }   
          else if (s % 3 == 2) {
             trafficLight = "green";
           }
           else if (s % 1 == 0) {
             trafficLight = "yellow";
           }
    
    lamp.innerHTML += `<div class="traffic" id="traffic` + s + `">
                      <div class="lampu buled ${trafficLight}"></div>
                      <button class="button buled" data-index="`+ s + `"></button></div>`
    }
    function toggleLight(index) {
    let serliCantik = document.getElementById('traffic' + index).querySelector('.lampu');
    serliCantik.classList.toggle('on')
    }
    lamp.addEventListener('click', function (e) { 
        if (e.target.classList.contains('button')) {
         let index = e.target.attributes['data-index'].value
         toogleLight(index)}
    });
Posted
Updated 20-Sep-22 1:14am
Comments
Richard MacCutchan 20-Sep-22 7:46am    
So what is your problem?
Richard Deeming 20-Sep-22 8:16am    
Aside from the fact that you haven't actually asked a question, why would your school be teaching your to go backwards? To take valid modern code, and mangle it to add a dependency on a 100Kb+ library that's not actually needed in any modern browser? For example, see: You Might Not Need jQuery[^]

1 solution

While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900