4

I have the JS-code:

$("#select_bank").change(function () {
  selected_bank = $("#select_bank option:selected").text();

  $.ajax({
    type: 'POST',
    dataType: 'html',
    data: {
      selectedBank: selected_bank
    },
    url: '<?= base_url() . 'atm/select_region'; ?>',
    success: function (list_regions) {
      foreach(keyVar in list_regions) {
        alert(list_regions[keyVar]);
      }
    }
  });
});

On callback "succes"s I get the array from server's script - in alert I see the "Array" - so I want to iterate via this array on client-side like I coded above, but when doing I get the error in console - "var keyVar is not defined". As I understand I need to typecast the list_regions param as array or some another way to fix it. Please, how to make it better? Thanks!

upd: enter image description here

1
  • 1
    Try changing 'foreach' to 'for' Commented Sep 1, 2012 at 6:15

4 Answers 4

6

If I am right you cannot transform the foreach loop into jquery in that way.

You should use .each to iterate the values

$.each(list_regions, function(index, value) {
  alert(value);
});

You can find more information here.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for propose, but it works as I've mentioned at my first comment - "Seems to be I have the trouble on my server's script: I get the sequense of alerts with the letters "A","r","r","a","y" and not the value's of array ?"
4

Javascript doesn't have foreach construction. Use $.each method of jQuery

6 Comments

Eugene is trying to use a JavaScript construct, and you jumped to telling him the jQuery solution. Just because Eugene is already using jQuery is not a good reason to advocate that they overlook the language's built-in construct.
@Mythril, but $.each() is much more userful in this case
Exactly how is it more useful?
Please, don't argue. Just I'd like to stream to use the pure JavaScript instead of Jquery to make some simple actions, when it's possible in rather simple way)
@Daniil, both of which are available in a for..in looop
|
0

Try this:

$("#select_bank").change(function(){
    // The following line was missing a var declaration,
    // Making it an implicit global
    var selected_bank = $("#select_bank option:selected").text();

    $.ajax({
        type:'POST',
        dataType:'html',
        data: { selectedBank: selected_bank },
        url:'<?=base_url().'atm/select_region'; ?>',
        success:function(list_regions){
            var keyVar;
            for(keyVar in list_regions) {
                if (list_regions.hasOwnProperty(keyVar)) {
                    alert(list_regions[keyVar]);
                }
            }
        }
    });

});

3 Comments

Seems to be I have the trouble on my server's script: I get the sequense of alerts with the letters "A","r","r","a","y" and not the value's of array ?
My guess would be that base_url() is returning a PHP array which is being cast to a string as "Array", which is then being iterated over letter by letter in JavaScript
I've updated the post to show what I get on JS-script. But to do it - I've made the dirty trick in CodeIgnater, but I really want to avoid such bad way...
0

Use $.each method of jQuery, not used foreach.

$.each(res.keyVar, function(index, row) {
   alert(row);
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.