0

I'm having a hard time in getting the data. I don't know where I went wrong.

Button:

<button class="btn btn-primary" style="font-size: 10px;" onclick="save()">Save</button>

Data coming from:

//index is dynamic from another js
var hide = '<label><input type="radio" class="menuOptions" name="save' + index + '" id="access' + index + '" value="0" ' + (access == 0 ? 'checked' : (access == 3 ? 'disabled' : '')) + ' <?php isset($access) ? ($access == 1 ? "disabled" : "") : "disabled" ?>/>Hide</label>';

function save() {
    var a = [];

    for (var i = 0; i != data.length; i++) {
        a.push({
            'value': $('input[name="save' + i + '"]:checked').val()
        });
    }
    console.log(a)
}

Data: When I console.log(a)

[Object, Object, Object, Object]
 0: Object
    a: 2
    title: 'MyTitle'
    __proto__: Object
 1: Object
    a: 2
    title: 'AnotherTitle'
    __proto__: Object

So on and so forth.

Tushar
  • 85,780
  • 21
  • 159
  • 179
aldrin27
  • 3,407
  • 3
  • 29
  • 43
  • 1
    What is undefined??? BTW, i don't understand what you mean in question's title, because `Output undefined always returns undefined`, seems logic after all – A. Wolff Aug 24 '15 at 08:54
  • I've updated my question. – aldrin27 Aug 24 '15 at 08:56
  • Ok so, how do you output it??? Again, what returns `undefined`?`save()` method or what? If `save()` method, returns `a` as in Tushar's answer. If you are outputting `a` outside `save()` method, then ya, it is `undefined` outside `save()` scope. `var a = [];` you are defining `a` local to function wrapper scope http://stackoverflow.com/questions/500431/what-is-the-scope-of-variables-in-javascript – A. Wolff Aug 24 '15 at 08:57
  • data looks mysterious. It is not obvious how it is being injected into the scope or what it represents – shanks Aug 24 '15 at 09:56

2 Answers2

4

Because you haven't returned anything from function by default undefined is returned.

My primary guess is that data and checked checkboxes has no connection at all. So, when you loop using the data.length, there is no checked checkboxes, hence you might be getting undefined. Otherwise, you've got [] as output of log.

Anyway, I've also updated the save().

function save() {
    var a = []; // Initialize array

    var checkedElements = $('input[name="save"]:checked'); // Get all save checked checkboxes

    for (var i = 0; i < checkedElements.length; i++) {
        a.push({
            value: checkedElements.eq(i).val() // Add value of checked checkboxes in array
        });
    }

    return a; // return array from function
}
Tushar
  • 85,780
  • 21
  • 159
  • 179
  • Yes. It is coming from the radioboxes. When i change it still gives me undefined. I've updated my question. – aldrin27 Aug 24 '15 at 09:17
  • @aldrin27 Can you please create jsfiddle.net demo. It'll be easier to solve the problem – Tushar Aug 24 '15 at 09:18
0

Probably becasuse the data is out of scope. If you try to log data in the function it will probably be undefined, and the loop has nothing to iterate over. Either pass the data to the funciton save(data) or make the data availible in the "above" scope

BobbyTables
  • 4,481
  • 1
  • 31
  • 39