Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find elements that are not deeper than a selector?

I am building a jQuery plugin to manage form collections. The plugin aims to add add, remove, move up and move down buttons to alter that collection.

  • A collection's root node always contains a selector, such as .collection.

  • A button can be anything as soon as it has the .add class

I implemented min and max options, so add and remove buttons disappear accordingly. My problem comes up when I try to manage a collection of form collections: how to select only the add buttons that refers to the right collection?

To simplify the problem, look at the following HTML code:

<div class="collection">
  <div>something</div>
  <div>something</div>
  <div>
    <div class="add">+</div>
  </div>
  <div>something</div>
  <div class="collection">
    <div>something</div>
    <div>something</div>
    <div>
      <div class="add">+</div>
    </div>
    <div>something</div>
  </div>
</div>

Keep in mind that the button can be arbitrary deep: collection is built by an user and I don't know where can be the button in the dom. BTW, it is deeper than the .collection, that's all I know.

How to select all add buttons until the second .collection, but not further?

For those interested, this plugin is available (but in active dev) here.

like image 955
Alain Tiemblo Avatar asked Feb 28 '15 17:02

Alain Tiemblo


People also ask

What CSS selector should we use if we want to select all elements but not the specified class or selector?

For selecting everything but a certain element (or elements). We can use the :not() CSS pseudo class.

Which selector helps to select all elements of a document except paragraphs?

The :not() selector selects all elements except the specified element. This is mostly used together with another selector to select everything except the specified element in a group (like in the example above).

How do you select all p elements inside a div element?

Use the element element selector to select all elements inside another element.


2 Answers

I will assume you have a reference to the .collection object that you want to find the add buttons for in a variable called target. If so, you can do it like this:

target.find(".add").filter(function(i, element) {
   return $(element).closest(".collection").get(0) === target.get(0);
});

This finds all the .add buttons that are in a given .collection and then removes any who are contained in a nested .collection instead of directly in the target .collection.

like image 107
jfriend00 Avatar answered Oct 09 '22 16:10

jfriend00


Try

$(".add").not($(".collection:gt(0) .add"));

Note,

Utilizing jQuery .not()'s .not( selector ) , where selector is selctor string

.not( selector ) version added: 1.0

selector Type: Selector or Element or Array A string containing a selector expression, a DOM element, or an array of elements to match against the set.

$(".add").not(".collection:gt(0) .add") http://jsfiddle.net/47wc5L96/21/

did not appear to return same results as .not( selection ) , where selection is jQuery object

.not( selection ) version added: 1.4

selection Type: jQuery An existing jQuery object to match the current set of elements against.

$(".add").not($(".collection:gt(0) .add")); http://jsfiddle.net/47wc5L96/20/


console.log($(".add").not($(".collection:gt(0) .add")));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
<div class="collection">
  <div>something</div>
  <div>something</div>
  <div>
<div class="add">+</div>
  </div>
  <div>something</div>
  <div class="collection">
<div>something</div>
<div>something</div>
<div>
  <div class="add">+</div>
</div>
<div>something</div>
  </div>
</div>
like image 22
guest271314 Avatar answered Oct 09 '22 16:10

guest271314