0

I set a new property on a table element (each table should have the property selectedRow, which is a pointer to the tr element that is selected), but actually between calls of my click handler, that property becomes null:

 $("table.grid").each(function () {
        this.selectedRow = null;
    });

    var selectRow = function (tr) {

        var table = tr.parents("table").get();

        if (table.selectedRow == tr.get()) return;

        // table.selectedRow still NULL!!!!!!!!!!!
        if (table.selectedRow) {
            var unselect = $(table.selectedRow);
            unselect.removeClass('selectedChilds');
            unselect.prev('tr').removeClass('siblingUpChilds');
            unselect.next('tr').removeClass('siblingDownChilds');
        }

        table.selectedRow = tr.get();

        tr.addClass('selectedChilds');
        tr.prev('tr').addClass('siblingUpChilds');
        tr.next('tr').addClass('siblingDownChilds');

    }

    $('table.grid tr').click(function (e) {
        selectRow($(e.delegateTarget));
    });
5
  • 1
    are you using a plugin? Tables don't have a selectedRow property. Commented Dec 4, 2013 at 23:10
  • no i set up that property... Commented Dec 4, 2013 at 23:15
  • ok, the problem is that the property is attached to the variable that stores your table, not to the table itself. Commented Dec 4, 2013 at 23:22
  • @Christophe: but that variable is pointer to dom object, so ... it is actually set on that table i think Commented Dec 4, 2013 at 23:38
  • it doesn't, because DOM tables don't have a selectedRow property. The correct way is to set a data attribute (not a property), as in the accepted answer. Commented Dec 4, 2013 at 23:52

1 Answer 1

1

Use data(key[, value]) to set/get properties:

$('#my-elem').data('selected_row', '...');
Sign up to request clarification or add additional context in comments.

Comments

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.