2

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.

1
  • what is the value of container Commented Jun 5, 2015 at 3:43

1 Answer 1

1

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

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

1 Comment

jajajajaja that was exactly the answer to my problem, a beginner problem but yet it makes me suffer, jajaj I don´t know why I dind't thing in that. Thank you so much @ArunPJohny

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.