User999999
User999999

Reputation: 2520

JQuery - $(#).remove() vs document.getelementbyid().remove

Originally i had a remove function like this:

function ViewWorkflowDetail(btn, workflowId) {

      $("#workflowDetailPanel").remove();

      if (document.getElementById("workflowDetailPanel") == null) {
             // Do something usefull here
      }
}

Which worked brilliantly. Yet (in the spirit of using as much JQuery as possible) I changed it to:

function ViewWorkflowDetail(btn, workflowId) {

      $("#workflowDetailPanel").remove();

      if ($("#workflowDetailPanel") == null) {
             // Do something usefull here
      }
}

But right now $("#workflowDetailPanel") is never null anymore. If i change it back again (to document.getElementById), then there is no problem anymore. Why does the second option keeps on finding that div? Are the JQuery objects somehow maintained in some sort of cache?

Note: Exactly the same setup/data were used to test both cases.

Upvotes: 0

Views: 540

Answers (1)

Anton
Anton

Reputation: 32581

It will never be null, since jQuery returns an empty array if the element does not exist, you must check the length of the array

  if ($("#workflowDetailPanel").length > 0) {
         // Do something usefull here
  }

Upvotes: 7

Related Questions