Thomas
Thomas

Reputation: 79

Check if div has Elements and does not have certain Elements

I already had a similar thread opened except the condition that the div does not have a second Element in it. So my last Question was this:

I have some Code which looks like this

<p class="elementWrap">
  <label>Phone</label>
  <input class="form_elem" type="text" name="form_phone">
  <span class="form_required">*</span>
</p>

and sometimes like this

<p class="elementWrap">
  <label>Name</label>
  <input class="form_elem" type="text" name="form_name">
</p>

rgraham posted this solution which works fine

$(".elementWrap").filter(function() {
    return $(this).children(".form_required").length;
}).find("label").css({'width':'auto','margin':'0px'});

But now I dont want to apply this code on Labels which have Inputs with the type Radio. I thought about something like return the length of children .form_required and not input type radio. But dont know how to write this as code. =/

Upvotes: 0

Views: 351

Answers (3)

Abdennour TOUMI
Abdennour TOUMI

Reputation: 93203

$(".elementWrap:has(.form_required)").filter($(".elementWrap:not(:has(:radio))"))

Known that $one.filter($two)$one ∩ $two

DEMO

Upvotes: 0

Christian Gollhardt
Christian Gollhardt

Reputation: 17014

From the Jquery Docs:

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>has demo</title>
    <style>
        .full {
            border: 1px solid red;
        }
    </style>
    <script src="//code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<ul>
    <li>Does the UL contain an LI?</li>
</ul>
<script>
    $("ul").append("<li>" +
        ( $("ul").has("li").length ? "Yes" : "No" ) +
        "</li>");
    $("ul").has("li").addClass("full");
</script>
</body>
</html>

http://api.jquery.com/has/

Upvotes: 0

adeneo
adeneo

Reputation: 318212

$(".elementWrap").filter(function() {
    var has_required = $(this).children(".form_required").length > 0,
        no_radio    = $(this).find(':radio').length < 1;

    return has_required && no_radio;
})

Upvotes: 1

Related Questions