15

Is there a foreach code in JQuery as in PHP? I have a code in php,like

<?php foreach ($viewfields as $viewfield): ?>
if ("<?php echo $viewfield['Attribute']['required'];?>" == 'true') {
  $("<span class='req'><em> * </em></span>").appendTo("#fb_contentarea_col1down21 #label<?php echo $viewfield['Attribute']['sequence_no']?>");
}

if (<?=$viewfield['Attribute']['type'];?> == 'text' || <?=$viewfield['Attribute']['type'];?> == 'date' || <?=$viewfield['Attribute']['type'];?> == 'number') {
  $("<input id=input<?=$viewfield['Attribute']['sequence_no'];?> type= 'text' style= 'width:<?=$viewfield['Attribute']['size'];?>px' data-attr=<?=$viewfield['Attribute']['type'];?> ></input><br>").appendTo("#fb_contentarea_col1down21 #<?=$viewfield['Attribute']['sequence_no'];?>");
} 
else if (<?=$viewfield['Attribute']['type'];?> == 'textarea') {
  $("<textarea style= 'width:<?=$viewfield['Attribute']['size'];?>px' data-attr=<?=$viewfield['Attribute']['type'];?> id=input<?=$viewfield['Attribute']['sequence_no'];?>></textarea><br>").appendTo("#fb_contentarea_col1down21 #<?=$viewfield['Attribute']['sequence_no'];?>");
}
<?php endforeach; ?>

Is there any equivalent of foreach in Jquery? How can I accomplish this same functioality in jQuery?

EDIT 1:

I thought it worked but I get an error. The code and the error message is given below.

for (<?=$viewfield;?> in <?=$viewfields;?>) {

  if ("<?=$viewfield['Attribute']['required'];?>" == 'true') {
    $("<span class='req'><em> * </em></span>").appendTo("#fb_contentarea_col1down21 #label<?php echo $viewfield['Attribute']['sequence_no']?>");
  }

  if (<?=$viewfield['Attribute']['type'];?> == 'text' || <?=$viewfield['Attribute']['type'];?> == 'date' || <?=$viewfield['Attribute']['type'];?> == 'number') {
    $("<input id=input<?=$viewfield['Attribute']['sequence_no'];?> type= 'text' style= 'width:<?=$viewfield['Attribute']['size'];?>px' data-attr=<?=$viewfield['Attribute']['type'];?> ></input><br>").appendTo("#fb_contentarea_col1down21 #<?=$viewfield['Attribute']['sequence_no'];?>");
  } 
  else if (<?=$viewfield['Attribute']['type'];?> == 'textarea') {
    $("<textarea style= 'width:<?=$viewfield['Attribute']['size'];?>px' data-attr=<?=$viewfield['Attribute']['type'];?> id=input<?=$viewfield['Attribute']['sequence_no'];?>></textarea><br>").appendTo("#fb_contentarea_col1down21 #<?=$viewfield['Attribute']['sequence_no'];?>");
  }
}

Error message:

syntax error for( in Array)

Can someone help me..

biberman
  • 5,606
  • 4
  • 11
  • 35
Angeline
  • 2,369
  • 22
  • 68
  • 107
  • 1
    JavaScript and PHP can't interact... PHP creates the JavaScript code before it is sent to the client. After it is sent to the client, it is executed. If you look in the source, you'll see that what PHP outputted is `for( in Array){` (because `$viewfield` is an empty variable, and `$viewfields` is a PHP array which, when represented as a string shows as `Array`.) You'll need to do it all in JavaScript or all in PHP, you can't mix the two languages. – Blixt Jul 29 '09 at 11:20
  • but due to the use of php within jquery, i get an error message in the document.ready() close tag. If I remove that for each php code,the error is gone, and the document.location works fine. So now how to write that for each code in JQuery? – Angeline Jul 30 '09 at 04:42

5 Answers5

45

The $.each function is similar.

It allows you to iterate arrays using a callback function where you have access to each item:

var arr = [ "one", "two", "three", "four", "five" ];


$.each(arr, function(index, value) {
  // work with value
});

Maybe is useful to know, if you want to break the loop, you can do it with return false; or if you want to skip only one iteration (continue), you return true;

Christian C. Salvadó
  • 807,428
  • 183
  • 922
  • 838
26

If you want to iterate an object, I would recommend the JavaScript variant:

for (var key in obj) {
    alert(key + ': ' + obj[key]);
}

You can also iterate objects in jQuery like this:
Note! Doing this is pretty pointless unless you think this syntax is much simpler to maintain. The below syntax has much more overhead than the above, standard JavaScript, for-loop.

$.each(obj, function (key, value) {
    alert(key + ': ' + value);
});

To iterate arrays, this is how you do it in standard JavaScript (assuming arr is the array):

for (var i = 0, l = arr.length; i < l; i++) {
    alert(i + ': ' + arr[i]);
}

To do it in jQuery, you can do it like this:

$.each(arr, function (index, value) {
    alert(index + ': ' + value);
});
Blixt
  • 49,547
  • 13
  • 120
  • 153
5

There is jQuery.each.

Chetan S
  • 23,637
  • 2
  • 63
  • 78
5

Javascript supports the for(data in data_array) syntax. jQuery also has a $.each function (as already mentioned)

Torandi
  • 1,615
  • 2
  • 9
  • 12
4

Jquery operating on selectors:

$('a').each(function() {
    $(this).click(function(e) {
       e.preventDefault()
       var href = this.href;
       open(href);
    });
    // operate on the anchor node.
});

jQuery direct $.each:

var a = ['one', 'two'];

$.each(a, function() {
    alert(this)
});

JS: Vanilla for loop

 for ( var i = 0, len = 10; i<l; ++i ) {
    alert(i)
 }

JS #2: vanilla for

 var humanLimbs = ['arms', 'legs'];
 for ( var limb in humanLimbs ) {
     if ( humanLimbs.hasOwnProperty(limb) ) {
        alert( limb )
     }
 }

Js #3: infinite loop

for (;;) { alert(1) } // dont try this :p
meder omuraliev
  • 183,342
  • 71
  • 393
  • 434