Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If <code>intersection</code> is a list of IDs, it's really simple.</p> <p>Using straight DOM methods (no jQuery), you can use <a href="http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/core.html#ID-getElBId" rel="nofollow"><code>getElementById</code></a> to look up each element, <a href="http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/core.html#ID-1060184317" rel="nofollow"><code>parentNode</code></a> to get its parent node, and then <a href="http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/core.html#ID-1734834066" rel="nofollow"><code>removeChild</code></a> to remove it:</p> <pre><code>var index; for (index = 0; index &lt; intersection.length; ++index) { elm = document.getElementById(intersection[index]); elm.parentNode.removeChild(elm); } </code></pre> <p>But it's worth noting that jQuery works around <a href="http://blog.niftysnippets.org/2010/03/by-any-other-name-would-smell-as-sweet.html" rel="nofollow">bugs in IE6's and IE7's implementation</a> of <code>getElementById</code>, so if you're already using jQuery, you may want to use it for this as well to get the advantage of those workarounds.</p> <p>Or use jQuery's <a href="http://api.jquery.com/remove/" rel="nofollow"><code>remove</code></a>:</p> <pre><code>var index; for (index = 0; index &lt; intersection.length; ++index) { $("#" + intersection[index]).remove(); } </code></pre> <p>Or if you like, use jQuery's <a href="http://api.jquery.com/jQuery.each/" rel="nofollow"><code>each</code></a> with <code>remove</code>:</p> <pre><code>$.each(intersection, function(index, value) { $("#" + value).remove(); }); </code></pre> <p><strong>Update 2011/04/29</strong>: Or you can take advantage of <code>Array#join</code> to create a selector in the form <code>#id1, #id2, #id3</code> and them remove them with one call:</p> <pre><code>$('#' + intersection.join(', #')).remove(); </code></pre> <hr> <p><strong>Update</strong>: There's some discussion of making sure that the elements are <code>li</code> elements. If you really need to do that, here are the three above, modified:</p> <p>Straight DOM (added the <code>if</code>):</p> <pre><code>var index; for (index = 0; index &lt; intersection.length; ++index) { elm = document.getElementById(intersection[index]); if (elm.tagName === "LI") { elm.parentNode.removeChild(elm); } } </code></pre> <p>jQuery options (literally just add "li" in front of "#" to say "the element with this ID, but only if it's a <code>li</code>"):</p> <p>For loop:</p> <pre><code>var index; for (index = 0; index &lt; intersection.length; ++index) { $("li#" + intersection[index]).remove(); } </code></pre> <p>Using <code>each</code>:</p> <pre><code>$.each(intersection, function(index, value) { $("li#" + value).remove(); }); </code></pre> <p><strong>Update 2011/04/29</strong>: Using <code>Array#join</code>:</p> <pre><code>$('li#' + intersection.join(', li#')).remove(); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload