James Wilson
James Wilson

Reputation: 5150

Having trouble cleaning up my code

I have 4 functions that do something I feel one can do.

function getDownline(rankid, args, type) {
    $('body').append('<div class="modal">');
    $.getJSON('message_center_getMessageLists.asp', args, function (json) {
        var options = '';
        $.each(json.downline, function (i, item) {
            options += '<option value="' + item.distID + '" title="' + item.name.toLowerCase() + ' (' + item.distID + ')" >' + item.name.toLowerCase() + ' (' + item.distID + ')</option>';
        });

        $('#fromList').find('option')
            .remove()
            .end();

        $('#fromList').append(options);
    })
    .success(function () { $('.modal').remove(); })
    .error(function () { 
        alert("There was an error while trying to make this request;  If it persists please contact support"); 
        $('.modal').remove(); 
     });
 }

The only real difference is the json.downline it can be 4 different things. I added the type param. Which I was trying to use to pass in downline, sponsor, pSponsor or monitorlist.

But I can't seem to just do json.type, or json. + type any idea what I am missing?

Upvotes: 0

Views: 45

Answers (1)

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324650

json[type] will get the property named by type.

Upvotes: 1

Related Questions