Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

115
Views
Is it possible to loop through multiple ID'S to change an attribute
<script type="text/javascript">
    var packetStorageNum = 0;

    function increase_stored(){
        packetStorageNum++;
        if(packetStorageNum>6){
            packetStorageNum = 6
        }
    }
    function decrease_stored(){
        packetStorageNum--;
        if(packetStorageNum<0){
            packetStorageNum = 0
        }
    }
    function run()
    {
        for(let i = 1;i<packetStorageNum+1;i++){
            document.getElementById('i').style.display='block';
        }
        
    }
</script>
<body>
    <div class="container">
        <p>number of packets in storage:</p>
        <div class="buttons">
            <input type="button" onClick="increase_stored()" value="Increase"/>
            <input type="button" onClick="decrease_stored()" value="Decrease"/>
        </div>
        <input type="button" onClick="run()" value="Run"/>
    </div>

    <div>
        <div><img src="images/box.jfif" alt="" id="1" style="display:none ;"></div>
        <div><img src="images/box.jfif" alt="" id="2" style="display:none ;"></div>
        <div><img src="images/box.jfif" alt="" id="3" style="display:none ;"></div>
        <div><img src="images/box.jfif" alt="" id="4" style="display:none ;"></div>
        <div><img src="images/box.jfif" alt="" id="5" style="display:none ;"></div>
        <div><img src="images/box.jfif" alt="" id="6" style="display:none ;"></div>         
    </div>
</body>

The code is to 'turn on' certain images as the increase button is clicked and to hide them when its not. I gave the boxes id's of numbers and was trying to use a For loop to go through the numbers so when the value of boxes is say 5, box 1-5 will be visible.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You need to use i as the variable - currently you're looking for an element with the id #i rather than #1, #2 etc.

document.getElementById(i.toString()).style.display = "block";
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!