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'];?>");
}
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..
jQuery Misc each() Method The each() method specifies a function to run for each matched element. Tip: return false can be used to stop the loop early.
Answer: Use the jQuery. each() function each() or $. each() can be used to seamlessly iterate over any collection, whether it is an object or an array. However, since the $. each() function internally retrieves and uses the length property of the passed array or object.
To break a $. each or $(selector). each loop, you have to return false in the loop callback. Returning true skips to the next iteration, equivalent to a continue in a normal loop.
The forEach() method calls a function for each element in an array. The forEach() method is not executed for empty elements.
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;
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);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With