Home > Software design >  create and append a span inside multiple div of same class
create and append a span inside multiple div of same class

Time:12-03

<div data-component-id="nwv4kv9j5ct9"  data-component-status="major_outage" data-js-hook="" id="nwv4kv9j5ct9">

   <span >JHP</span>
   <span ><a href="" target="_blank" 
      onclick="event.stopPropagation()">Tier 2</a></span>
 
   <span  style="display: none;">?</span>
 
   <span  title=""><span >failing</span></span>
 
   <span ></span>
 
</div>
<div data-component-id="rtv4kv9j5cyu"  data-component-status="major_outage" data-js-hook="" id="nwv4kv9j5ct9">

   <span >PHS</span>
   <span ><a href="" target="_blank" 
       onclick="event.stopPropagation()">Tier 2</a></span>
 
   <span  style="display: none;">?</span>
 
   <span  title=""><span >degrading</span></span>
 
   <span ></span>
 
</div>
......
......

I need to create and append a span tag <span >Type</span> inside each div with class name component-inner-container. It should be after

$('.component-inner-container').each(function () {
        var span = $('<span />').attr('className', 'type')
        span.appendTo(".component-inner-container");

    });

I'm stuck here and couldn't able to find right approach

Edit : added span should be after for example <span >PHS</span>

CodePudding user response:

You can try do it this way:

$('.component-inner-container').each(function() {
  var span = $('<span />').attr('class', 'type table-item-type').text("Type")
  span.appendTo(this);
});

I've changed your span.appendTo(".component-inner-container"); to span.appendTo(this), since I think you only want one span in each container.

Demo

$('.component-inner-container').each(function() {
  var span = $('<span />').attr('class', 'type table-item-type').text("Type")
  span.appendTo(this);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div data-component-id="nwv4kv9j5ct9"  data-component-status="major_outage" data-js-hook="" id="nwv4kv9j5ct9">

  <span >JHP</span>
  <span ><a href="" target="_blank" 
      onclick="event.stopPropagation()">Tier 2</a></span>

  <span  style="display: none;">?</span>

  <span  title=""><span >failing</span></span>

  <span ></span>

</div>
<div data-component-id="rtv4kv9j5cyu"  data-component-status="major_outage" data-js-hook="" id="nwv4kv9j5ct9">

  <span >PHS</span>
  <span ><a href="" target="_blank" 
       onclick="event.stopPropagation()">Tier 2</a></span>

  <span  style="display: none;">?</span>

  <span  title=""><span >degrading</span></span>

  <span ></span>

</div>

CodePudding user response:

The issue is because in your loop you append to every .component-inner-container. Therefore you end up with N copies in each parent, where N is the length of your loop.

To fix the problem, just call append() directly on the jQuery object holding all .component-inner-container elements.

Also note that you should use prop() when setting the className property, and also that it's better to use addClass() in either case.

$('.component-inner-container').append($('<span />').addClass('type table-item-type').text('foo'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div data-component-id="nwv4kv9j5ct9"  data-component-status="major_outage" data-js-hook="" id="nwv4kv9j5ct9">
  <span >JHP</span>
  <span >
    <a href="" target="_blank" onclick="event.stopPropagation()">Tier 2</a>
  </span>
  <span  style="display: none;">?</span>
  <span  title="">
    <span >failing</span>
  </span>
  <span ></span>
</div>
<div data-component-id="rtv4kv9j5cyu"  data-component-status="major_outage" data-js-hook="" id="nwv4kv9j5ct9">
  <span >PHS</span>
  <span >
    <a href="" target="_blank" onclick="event.stopPropagation()">Tier 2</a>
  </span>
  <span  style="display: none;">?</span>
  <span  title="">
    <span >degrading</span>
  </span>
  <span ></span>
</div>

CodePudding user response:

$newSpan = $("<span />").addClass("type");
$('.component-inner-container').append($newSpan);

It will append the span to all elements with class "component-inner-container".

  • Related