Ruben R.
Ruben R.

Reputation: 92

Value array selector as a variable jquery

I hope someone can help me. I been crashing down my head trying to end up with a solution for this, here is the deal, I have a function passing through some variables like this:

function createTaskView(results, listName, container, url, groupby) {

The variables are so simples, "results" it's an array, the other ones are simply strings. So far so good.

Then I start an $.each loop in the array like this:

function createTaskView(results, listName, container, url, groupby) {

    $.each(results, function (index, task) {
        var tr = '<tr>' +
                   '<td><a href="'+ url +'Lists/'+ listName +'/EditForm.aspx?ID='+ task.ID +' ">' + task.ID + '</a></td>' +
                   '<td>' + task.Year + '</td>' +
                   '<td>' + task.Site_x0020_Name + '</td>' +
                '</tr>';

        $(container + ' .' + task.groupby).append(tr);

    });

});

The problems starts here

$(container + ' .' + task.groupby).append(tr);

I know there is no key in my DB call "groupby", I meant to use the string value passed into the function in here:

function createTaskView(results, listName, container, url, groupby) {

So, if the variable groupby is like this:

var groupby = Year;

I want this:

task.groupby

To be equal like this:

task.Year

I don't know if I make myself clear. Please someone help me.

Thanks.

Upvotes: 2

Views: 193

Answers (1)

Arun P Johny
Arun P Johny

Reputation: 388316

You need to use bracket notation to access a property whose name is stored in a variable so

$(container + ' .' + task[groupby]).append(tr);

Also make sure that the value of container is a proper selector like #myid for an id selector not just myid

Upvotes: 1

Related Questions