5

I manage to get a collection of cells in a row when this row is clicked with the following:

$('.NamesGridClass tbody tr').bind('click', function() {
    var ch = $(this).children();
    alert(ch[0] + ' ' + ch[1]);
});

The above selection snippet successfully displays :

[object HTMLTableCellElement] [object HTMLTableCellElement]

I have tried ch[0].html(), ch[0].val(), ch[0].text(), and get errors. How can I get the content of my cells here ?

3 Answers 3

6

In order to use .html(), you need to turn those objects into jQuery objects:

$(ch[0]).html()

Same would apply to any other valid jQuery method you wish to apply.

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

1 Comment

@jQuery00, yes, of course, that does the same thing and with fewer characters. But the point is that .html() only works on a jQuery object. Both ch and $(ch[0]) are jQuery objects; ch[0] is not.
6

Please use native code

ch[0].innerHTML

3 Comments

It's a jquery-tagged question, so it seems inappropriate to suggest the OP go native. Yes, your "answer" works, but it doesn't help the OP understand why what he was trying doesn't.
Because jQuery .html() use the same method.
Plus a few odds and ends to ensure crossbrowser compatibility. .innerHTML is faster; .html() is safer.
2

When you access an item in an array you get from jQuery, you get back plain old javascript elements, not jquery elements. You can either use the javascript .innerHTML or double wrap the the value with the jquery selector. I would recommend using .innerHTML since its simpler, so

$('.NamesGridClass tbody tr').bind('click', function() {
        var ch = $(this).children();
        alert(ch[0].innerHTML + ' ' + ch[1].innerHTML);
    });

1 Comment

I would be careful about that recommendation. If the OP has to support older browsers, it's safer to use .html().

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.