0

I was wondering if I can select a div when it has an input field as a child.

Here is the HTML I am using:

<div class="xxx">
    <div class="panel panel-success">
      <div class="panel-heading">
        <h3 class="panel-title">my title</h3>
      </div>
      <div class="panel-body">
        <div class="row">
            <div class="col-md-5">some content</div>
            <div class="col.md-7">some content</div>
        </div>
        <div class="row">
            <div class="col-md-5">some content</div>
            <div class="col-md-3">
                <input type="text" class="form-control" id="date" />
            </div>
        </div>
      </div>
    </div>
</div>

Bootstrap adds a padding and margin to all div blocks if they have col-* classes. What I want to achieve is that, if a div contains an input field, then the margin and padding should be set to 0px.

So the main question is: "Can I select a div according to his content?"

I have tried div[class*="..."] and div[class^="..."], but neither is solving my problem.

Dwza
  • 6,494
  • 6
  • 41
  • 73

2 Answers2

3

Try to use :has() selector,

$('div:has(:input)')

Or you can use,

$(':input').filter(function(){
  return $(this).parent('div').length;
}).parent();
Rajaprabhu Aravindasamy
  • 66,513
  • 17
  • 101
  • 130
3

You can choose an alternate strategy. You can add a class to those div and use css rule to apply zero padding and margin.

<div class="col-md-3 zero-padding-margin">
            <input type="text" class="form-control" id="date" />
        </div>

And

  .zero-padding-margin {
margin: 0;
padding: 0;
}
Amit
  • 91
  • 3
  • this is how im dooing it atm. seems like i can't come around using extra classes for this. – Dwza Jul 21 '14 at 12:52