Question

I need to target every first div.bar. I went through many ways but with no success with jQuery, although it seemed easy at first.

I need this in order to wrap all the div.bar after, but this I can do with .wrapAll() afterwards.

<div id="aaa">
  <div class="bar" />
  <div class="bar" />
  <div class="bar" />
</div>
<span id="bbb">
          <div class="bar" />
          <div class="bar" />
          <div class="bar" />
        </span>
<div id="ccc">
  <div class="bar" />
  <div class="bar" />
</div>

Was it helpful?

Solution

You could use :first-child:

$('.bar:first-child').wrap('<div class="test"/>');
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top