Home > database >  execution script for several ID - loop?
execution script for several ID - loop?

Time:05-07

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" >
    <a  href="#">
      <div >plus1 <i ></i></div>
    </a>
</div>

<div id="2" >
    <a  href="#">
      <div >plusid2 <i ></i></div>
    </a>      
</div>

<div id="3" >
    <a  href="#">
      <div >plusid3 <i ></i></div>
    </a>
</div>

CodePudding user response:

  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" >
    <a  href="#">
      <div >plus1 <i ></i></div>
    </a>
</div>

<div id="2" >
    <a  href="#">
      <div >plusid2 <i ></i></div>
    </a>      
</div>

<div id="3" >
    <a  href="#">
      <div >plusid3 <i ></i></div>
    </a>
</div>

  • Related