mrrsb mrrsb - 1 year ago 62
Javascript Question

Loop URL and load using array depends on ID

I have a string list of id's where;

ids = '1,2,3,4,5'

just a representation of div

<div id="item_1">Text 1 - Suspended </div>
<div id="item_2">Text 2 - Suspended </div>
<div id="item_3">Text 3 - Suspended </div>
<div id="item_4">Text 4 - Suspended </div>

and converted to array (if you have a better solution to convert string to array please share, tia!)

array_id = ids.split(',');

My problem is that how can I loop the url and load it into a different div with array id?

function multipleLoadContent(url,id){
var ids = id;
var array_id = new Array();
array_id = ids.split(',');

// maybe loop here and apply where id is?
$("#item_"+array_id ).load(url);

returned text is a system generated output, example

for id "item_1" returned text "Text 1 - Active"
for id "item_2" returned text "Text 2 - Declined"
for id "item_3" returned text "Text 3 - Stale"

Answer Source

You can do a for loop to iterate over each element:

array_id.forEach(item) {
    $("#item_" + item).load(url);

This is a for-each loop which iterates over all elements in array_id. The current element's value is accessible through item. Consider:

var array_test = [1, 2, 3, 6]
array_test.forEach(function(item) {

This will log:


As expect because we loop over each element and log it.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download