Home > Enterprise >  Trying to update html with javascript
Trying to update html with javascript

Time:10-24

So here is my code that i need help with

<!DOCTYPE html>
<html lang="en">
 <head>
 <meta charset="UTF-8" />
 <meta name="viewport" content="width=device-width, initial-scale=1.0" />
 <link rel="stylesheet" href="style8.css" />
 <title>Progress Steps</title>
 </head>
 <body>
 <div >
 <div >
 <div  id="progress"></div>
 <div >1</div>
 <div >2</div>
 <div >3</div>
 <div >4</div>
 </div>
 <button  id="prev" disabled>Prev</button>
 <button  id="next">Next</button>
 </div>
 <script src="script8.js"></script>
 </body>
</html>

Here is the css

    @import url('https://fonts.googleapis.com/css?family=Muli&display=swap');
:root {
 --line-border-fill: #3498db;
 --line-border-empty: #383838;
}
* {
 box-sizing: border-box;
}
body {
 background-color: #1f1f1f;
 font-family: 'Muli', sans-serif;
 display: flex;
 align-items: center;
 justify-content: center;
 height: 100vh;
 overflow: hidden;
 margin: 0;
}
.container {
 text-align: center;
}
.progress-container { //im trying to use this
 display: flex;
 justify-content: space-between;
 position: relative;
 margin-bottom: 30px;
 max-width: 100%;
 width: 350px;
}
.progress-container::before { //im trying to use this
 content: '';
 background-color: var(--line-border-empty);
 position: absolute;
 top: 50%;
 left: 0;
 transform: translateY(-50%);
 height: 4px;
 width: 100%;
 z-index: -1;
}
.progress { //im trying to use this
 background-color: var(--line-border-fill);
 position: absolute;
 top: 50%;
 left: 0;
 transform: translateY(-50%);
 height: 4px;
 width: 0%;
 z-index: -1;
 transition: 0.4s ease;
}
.circle {
 background-color: #1f1f1f;
 color:#e2e2e2;
 border-radius: 50%;
 height: 30px;
 width: 30px;
 display: flex;
 align-items: center;
 justify-content: center;
 border: 3px solid var(--line-border-empty);
 transition: 0.4s ease;
}
.circle.active {
 border-color: var(--line-border-fill);
}
.btn {
 background-color: var(--line-border-fill);
 color: #fff;
 border: 0;
 border-radius: 6px;
 cursor: pointer;
 font-family: inherit;
 padding: 8px 30px;
 margin: 5px;
 font-size: 14px;
}
.btn:active {
 transform: scale(0.98);
}
.btn:focus {
 outline: 0;
}
.btn:disabled {
 background-color: var(--line-border-empty);
 cursor: not-allowed;
}

And here is the javascript. Im not really familiar with javascript. Kind of a newbie at it.

const progress = document.getElementById("progress");
const prev = document.getElementById("prev");
const next = document.getElementById("next");
const circles = document.querySelectorAll(".circle");
let currentActive = 1;
next.addEventListener("click", () => {
 currentActive  ;
 if (currentActive > circles.length) {
 currentActive = circles.length;
 }
 update();
});
prev.addEventListener("click", () => {
 currentActive--;
 if (currentActive < 1) {
 currentActive = 1;
 }
 update();
});
function update() { //here is the part i need help with
   
 }
 const actives = document.querySelectorAll(".active");
 progress.style.width =
 ((actives.length - 1) / (circles.length - 1)) * 100   "%";
 if (currentActive === 1) {
 prev.disabled = true;
 } else if (currentActive === circles.length) {
 next.disabled = true;
 } else {
 prev.disabled = false;
 next.disabled = false;
 }

Im trying to write a function to update the html when the back or next button is pressed and i need to use the progress css class. I have no idea how to implement it.

CodePudding user response:

You should be able to use the DOM control functions in JavaScript to fix your problem.

Send my answer. Sincerely

<!DOCTYPE html>
<html lang="en">
 <head>
 <meta charset="UTF-8" />
 <meta name="viewport" content="width=device-width, initial-scale=1.0" />
 <title>Progress Steps</title>
 </head>
 <style>
       @import url('https://fonts.googleapis.com/css?family=Muli&display=swap');
    :root {
    --line-border-fill: #3498db;
    --line-border-empty: #383838;
    }
    * {
    box-sizing: border-box;
    }
    body {
    background-color: #1f1f1f;
    font-family: 'Muli', sans-serif;
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100vh;
    overflow: hidden;
    margin: 0;
    }
    .container {
    text-align: center;
    }
    .progress-container { 
    display: flex;
    justify-content: space-between;
    position: relative;
    margin-bottom: 30px;
    max-width: 100%;
    width: 350px;
    }
    .progress-container::before { 
    content: '';
    background-color: var(--line-border-empty);
    position: absolute;
    top: 50%;
    left: 0;
    transform: translateY(-50%);
    height: 4px;
    width: 100%;
    z-index: -1;
    }
    .progress { 
    background-color: #3498db;
    position: absolute;
    top: 50%;
    left: 0;
    transform: translateY(-50%);
    height: 4px;
    width: 0%;
    z-index: -1;
    transition: 0.4s ease;
    }
    .circle {
    background-color: #1f1f1f;
    color:#e2e2e2;
    border-radius: 50%;
    height: 30px;
    width: 30px;
    display: flex;
    align-items: center;
    justify-content: center;
    border: 3px solid var(--line-border-empty);
    transition: 0.4s ease;
    }
    .circle.active {
    border-color: #3498db;
    }
    .btn {
    background-color: #3498db;
    color: #fff;
    border: 0;
    border-radius: 6px;
    cursor: pointer;
    font-family: inherit;
    padding: 8px 30px;
    margin: 5px;
    font-size: 14px;
    }
    .btn:active {
    transform: scale(0.98);
    }
    .btn:focus {
    outline: 0;
    }
    .btn:disabled {
    background-color: var(--line-border-empty);
    cursor: not-allowed;
    }
 </style>
 <body>
 <div > 
    <div id="content" style="border: 1px solid black; margin: 0 auto; width: 300px; height: 100px; background-color: white;">
        Content 1
    </div>
 <div >
 <div  id="progress"></div>
 <div >1</div>
 <div >2</div>
 <div >3</div>
 <div >4</div>
 </div>
 <button  id="prev" disabled>Prev</button>
 <button  id="next">Next</button>
 </div>
 </body>
 <script>
    const progress = document.getElementById("progress");
    const prev = document.getElementById("prev");
    const next = document.getElementById("next");
    const content = document.getElementById("content"); // added
    const circles = document.querySelectorAll(".circle");
    
    let currentActive = 1;

    next.addEventListener("click", () => {
        currentActive  ;
        
        if (currentActive > circles.length) {
            currentActive = circles.length;
        }

        update(currentActive);
    });

    prev.addEventListener("click", () => {
        currentActive--;
        
        if (currentActive < 1) {
            currentActive = 1;
        }

        update(currentActive);
    });

    function update(currentStep) { 
        //here is the part I fixed
        let stepItems = document.getElementsByClassName("circle");

        for (let i = 0; i < stepItems.length; i  ) {
            const stepItem = stepItems[i];
            
            if (stepItem.textContent == currentStep) {
                stepItem.classList.toggle("active");
            }
        }

        prev.toggleAttribute("disabled", false);
        next.toggleAttribute("disabled", false);

        if (currentStep == 1) prev.setAttribute("disabled", true);
        if (currentStep == 4) next.setAttribute("disabled", true);

        content.innerText = "Content"   currentStep;
    }

    const actives = document.querySelectorAll(".active");
    progress.style.width = ((actives.length - 1) / (circles.length - 1)) * 100   "%";
    
    if (currentActive === 1) {
        prev.disabled = true;
    } else if (currentActive === circles.length) {
        next.disabled = true;
    } else {
        prev.disabled = false;
        next.disabled = false;
    }
 </script>
</html>

  • Related