jQuery, Hidden Elements, and Why At Times I Hate It

So this is your situation, or at least it might be someday if you use jQuery:

You have a comment area that allows a user to view comments. Now this comment area is created using javascript (Read jQuery) only. When a user clicks on some kind of pager, a method is called to get a list of comments and recreate the comment table. Now you would think it would be a good idea to hide the table, recreate it, and then show it. Sounds like a decent way to go. So you try something like this:


  jQuery(commentTable).hide("slide", { direction: "up" }, 200 );


  //Get the comments and call a method to build the table.


  function buildTable(commentList, commentTable)

    //Take the list and iterate.  Create a row


     jQuery(commentTable).appendChild(someCreatedDivRowWithAComment)  //BOOM


Then you run it and bam you get an error saying that basically commentTable doesn’t exist. Now if this were WebForms and you did some kind of visible=”false” you would know that the markup for that table wouldn’t exist. However, in javascript style=”display:none;” doesn’t remove the element from the markup, it just hides it from the viewer. If you look at the source (Provided it’s not dynamically created) you will still see it in the mark up. So what’s the deal?

Well for some reason the creators of jQuery decided that there is a difference between a hidden element and a non hidden element to the point where you have to change your search (the jQuery() method is basically a search that looks for the element within the parenthesis) by adding on a “parameter”.


So if you do use the hide() method (Or fadeOut for that matter) you have to use the :hidden addon to find the stupid element. This can cause a big problem when things aren’t timed correctly. What does that mean? Well that 200 value I have in the hide method, that’s the time I want it to take to hide the element. Far as I can tell, and I’ve done some testing on this, the element is not :hidden until the animation completes. Which means there are 200 time units where it is not :hidden. Now if the time that it takes to get your comment list and start building the table is less than the time it takes to hide the element you’ve got an issue. Or conversely, if the 200 time units are up and it is now hidden, you’re really screwed because the normal search won’t work. You can try something like this:

    categoryBlockContainer = jQuery(this).children(".categoryBlockContainer:hidden");
    categoryBlockContainer = jQuery(this).children(".categoryBlockContainer");

And this works ok most of the time, but you still might run into a situation where it’s still trying to finish the hide animation. Beyond that, it seems kind of asinine to have something like that in the code. It should find the element at all times, hidden or not.

This really kills the whole idea of animation.

Between this and javascript’s bizarre variable scope, I’ve had some thoughts of changing my profession.

2 thoughts on “jQuery, Hidden Elements, and Why At Times I Hate It”

  1. Thank you for taking the time to post the jquery sliding rug on form elements problem.
    In my situation I found that I would use the .attr method to check for a form type, such as checkbox, and this worked regardless of whether the form was hidden:

    if($(this).attr(‘type’)==”hidden”) -finds a true hidden field inside a .each loop
    $(this).attr(‘type’)==”checkbox” && $(this).is(“:input:checked”) – finds a checked checkbox
    $(this).is(“textarea”) – finds a textarea, and if you are using CKEditor to take over textarea elements, and specifically you are using the CKEditor JQuery plugin ( ) you have to get the value of the element by using:
    value = $(‘#textareaname’).val();

    What I found extends what you are stating, and it might be helpful to someone.

    1. Appreciate the time you took to add on to this. Most likely you’re one of two people that actually come here but we can pretend that millions will read this.

Comments are closed.