jQuery: Find all DIVs containing specific elements

user1392897

I want to adjust the height of a div if certain conditions are true. Specifically, I want to find any .element divs that contain a textarea and in which the error message div exists.

$('.element').each(function() {
    if ( $(this).find('textarea') && $(this).find('.error-message') ) {
        $(this).css('min-height', '80px');
    }   
}); 

Given this structure, only the middle div should get the extra height:

<div class="element">
  <input type="text" />
</div>
<div class="element">
  <div class="error-message"></div>
  <textarea></textarea>
</div>
<div class="element">
  <input type="text" />
</div>
j08691

.length will be your friend here:

$('.element').each(function() {
  if ($(this).find('textarea').length && $(this).find('.error-message').length) {
    $(this).css('min-height', '80px');
  }
});
div {
  border: 1px solid #999;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="element">
  <input type="text" />
</div>
<div class="element">
  <div class="error-message"></div>
  <textarea></textarea>
</div>
<div class="element">
  <input type="text" />
</div>

While you're finding the proper elements with your selectors, in order to test them in an if condition you'll want to use .length which returns the number of elements in the jQuery object.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How do I find all files containing specific text on Linux?

From Dev

jQuery Displaying Different Elements (divs) when hovering over other specific elements

From Dev

Element Tree: How to find all child elements with a specific value

From Dev

Using jQuery to find list items containing specific text in nested lists

From Dev

jQuery prepend() to Elements that contain Divs

From Dev

Find a table containing specific text

From Dev

Find all words containing specific letters and special characters

From Dev

Find documents where ALL elements of an array have a specific value

From Dev

Jquery. Find all <tr> containing text from selected <option>

From Dev

Unable to find a button wrapping elements containing specific text for Selenium testing

From Dev

Find odd and even divs and put in containing div

From Dev

jQuery find all elements with class beneath parent, even in children elements

From Dev

How can I find and select all elements which have data attribute start with specific word with using jquery?

From Dev

jQuery - how to select elements not containing specific elelments

From Dev

Find specific table row containing substring text of a jquery variable of html

From Dev

JQuery find all data- elements

From Dev

jQuery find all elements start with specific id but not with another specific id

From Dev

Find a table containing specific text

From Dev

Find documents where ALL elements of an array have a specific value

From Dev

How to find all the dropped elements in jQuery

From Dev

Add class to all elements with a specific ID jQuery

From Dev

How can I find and select all elements which have data attribute start with specific word with using jquery?

From Dev

Find all elements containing a value in an attribute

From Dev

XSLT: Display all elements containing a specific child element (and only those elements)

From Dev

Selenium find all the elements which have two divs

From Dev

Can't find a way to select specific elements with JQuery

From Dev

Find specific table row containing substring text of a jquery variable of html

From Dev

Jquery to get all textarea elements with specific data

From Dev

How to find all domains containing specific string

Related Related

  1. 1

    How do I find all files containing specific text on Linux?

  2. 2

    jQuery Displaying Different Elements (divs) when hovering over other specific elements

  3. 3

    Element Tree: How to find all child elements with a specific value

  4. 4

    Using jQuery to find list items containing specific text in nested lists

  5. 5

    jQuery prepend() to Elements that contain Divs

  6. 6

    Find a table containing specific text

  7. 7

    Find all words containing specific letters and special characters

  8. 8

    Find documents where ALL elements of an array have a specific value

  9. 9

    Jquery. Find all <tr> containing text from selected <option>

  10. 10

    Unable to find a button wrapping elements containing specific text for Selenium testing

  11. 11

    Find odd and even divs and put in containing div

  12. 12

    jQuery find all elements with class beneath parent, even in children elements

  13. 13

    How can I find and select all elements which have data attribute start with specific word with using jquery?

  14. 14

    jQuery - how to select elements not containing specific elelments

  15. 15

    Find specific table row containing substring text of a jquery variable of html

  16. 16

    JQuery find all data- elements

  17. 17

    jQuery find all elements start with specific id but not with another specific id

  18. 18

    Find a table containing specific text

  19. 19

    Find documents where ALL elements of an array have a specific value

  20. 20

    How to find all the dropped elements in jQuery

  21. 21

    Add class to all elements with a specific ID jQuery

  22. 22

    How can I find and select all elements which have data attribute start with specific word with using jquery?

  23. 23

    Find all elements containing a value in an attribute

  24. 24

    XSLT: Display all elements containing a specific child element (and only those elements)

  25. 25

    Selenium find all the elements which have two divs

  26. 26

    Can't find a way to select specific elements with JQuery

  27. 27

    Find specific table row containing substring text of a jquery variable of html

  28. 28

    Jquery to get all textarea elements with specific data

  29. 29

    How to find all domains containing specific string

HotTag

Archive