Search code examples
jqueryobject

Is this valid object property creation syntax?



editEntry{}
$('#'+rowID+' td[data-field]').each(function() {
    var field = $(this).attr('data-field');
    var value = $(this).text();

    // handle the task complete checkbox
    if(field == 'complete') {
        if($(this).find('input').is(':checked')) {
            editEntry[field]=value;
            editEntry[field][checked]=true; // NOT SURE ABOUT THIS LINE
        }
    }



    editEntry[field]=value;
});

Solution

  • That will only work if checked is a variable.

    You're probably trying to write

    editEntry[field].checked=true;
    

    However, your code creates a property on a String instance.
    Don't do that; it's a very bad idea.

    Instead, you should probably write something like

    editEntry[field] = {
        value: value,
        checked: true
    };