execution script for several ID – loop?

Issue

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

  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", "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>

Alternative version using a css class

var data = "1,22,3,14";
let ids = data.split(",");

for(let id of ids)
{
    $("#"+id + " .plus").removeClass("plus"); 
    $("#"+id + " .1").addClass("highlight");
}
div.highlight
{
  color : red;
  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>

Answered By – Lee Taylor

Answer Checked By – Terry (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.