Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

execution script for several ID – loop?

I’m downloading ID from localStorage.getItem(1,22,3,14….).
I want every single ID to be executed in jQuery (if it exists on the website).
I can execute the code for one ID, but I don’t know what to do to make each ID after the decimal point executed. I try so but it doesn’t pass. I have to loop it, right? can anyone help?

var data = "1,22,3,14";
var format_id = data.replace(",", "");   
console.log('id=' +format_id); 

    $("#"+format_id+" .plus").removeClass("plus"); 
    $("#"+format_id+" .1").css("color", "red");
    $("#"+format_id+" .1").css("font-weight", "bold");
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="1" class="idkom1">
    <a class="punkt plus" href="#">
      <div class="1">plus1 <i class="fa fa-arrow-alt-circle-up"></i></div>
    </a>
</div>

<div id="2" class="idkom2">
    <a class="punkt plus" href="#">
      <div class="1">plusid2 <i class="fa fa-arrow-alt-circle-up"></i></div>
    </a>      
</div>

<div id="3" class="idkom3">
    <a class="punkt plus" href="#">
      <div class="1">plusid3 <i class="fa fa-arrow-alt-circle-up"></i></div>
    </a>
</div>

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  1. Split your string into an array using the split function.
  2. Loop through your ids and process each id in turn.
var data = "1,22,3,14";
let ids = data.split(",");

for(let id of ids)
{
    $("#"+id + " .plus").removeClass("plus"); 
    $("#"+id + " .1").css("color", "red");
    $("#"+id + " .1").css("font-weight", "bold");
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="1" class="idkom1">
    <a class="punkt plus" href="#">
      <div class="1">plus1 <i class="fa fa-arrow-alt-circle-up"></i></div>
    </a>
</div>

<div id="2" class="idkom2">
    <a class="punkt plus" href="#">
      <div class="1">plusid2 <i class="fa fa-arrow-alt-circle-up"></i></div>
    </a>      
</div>

<div id="3" class="idkom3">
    <a class="punkt plus" href="#">
      <div class="1">plusid3 <i class="fa fa-arrow-alt-circle-up"></i></div>
    </a>
</div>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading