简体   繁体   中英

Casting HTML Object elements?

I am trying to tick a checkbox which is inside the first cell of a tablerow.

var checkbox = $(this).find("td").eq(0);

returns an [Object object] but when I use the html() function it is clear that it returns only the code for the input element.

Is it possible to cast the variable checkbox so that I can then check it?

You're getting [Object object] because .eq() returns a jQuery object.

You're getting HTML code with .html() because that is a method of the jQuery object, which returns the HTML inside an element (similar to raw Javascript .innerHTML() ).

To get the raw HTML DOM element from a jQuery object, you can try something like:

var rawDomElement = $(this).find('td')[0];

EDIT

But if you want to check a checkbox, you can try calling .attr('checked', true) on a jQuery object representing the checkbox DOM element.

Try this :

var checkbox = $(this).find("td:first :input[type=checkbox]");
checkbox.prop('checked',!checkbox.prop('checked'));

And I recommend you use jquery's prop method instead of the attr one.

var checkbox = $(this).find("td:first").find(':checkbox');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM