June 28, 2010

Can't get jQuery hide working

Question by fearofawhackplanet

I need a sanity check as I’ve spent about an hour trying to figure this out!

getRows().each(function() {
    alert(this);     // alerts '[object HTMLTableRowElement]', nothing wrong here
    this.hide();     // row not hidden - wtf?
    alert('hidden'); // no alert - more wtf!
});

What can be wrong that calling hide() is bombing out?

Answer by Nick Craver

Inside the .each() this is a DOM element (HTMLTableRowElement), you need to wrap it to make it a jQuery object again (which has the .hide() method) like this:

$(this).hide();

Without this, you’re getting a method undefined error, because HTMLTableRowElement doesn’t have the .hide() method 🙂 This error is also why the alert isn’t firing afterwards, because execution stopped on the error.

Answer by Starx

try $(this).hide()

...

Please fill the form - I will response as fast as I can!