Append an element in every class

Renzo CJ

In JQuery ¿how can I append an img inside a div element in every second child of every div with the same class? I have this code and I want to insert a div tag between the h5 and p tags. The h5 tags are generic, they don't have any class or ID to reference, only their parent with tab-pane class.

<div class="tab-pane">
   <h5></h5>
   /*Here must be the new div in every tab-pane: <div><img></div>*/
   <p></p>
   <div></div>
   <ul></ul>
</div>
<div class="tab-pane">
   /*same content as before*/
</div>
<div class="tab-pane">
   /*same content as before*/
</div>
<div class="tab-pane">
   /*same content as before*/
</div>

So I tried this:

var paneWrap = document.createElement("div");
$(".tab-pane:nth-child(2)").append(paneWrap);

However the result was:

<div class="tab-pane">
   <h5></h5>
   <p></p>
   <div></div>
   <ul></ul>
</div>
<div class="tab-pane">
   <h5></h5>
   <p></p>
   <div></div>
   <ul></ul>
   <div></div> /*THE NEW DIV IS HERE!*/
</div>
<div class="tab-pane">
   <h5></h5>
   <p></p>
   <div></div>
   <ul></ul>
</div>
<div class="tab-pane">
   <h5></h5>
   <p></p>
   <div></div>
   <ul></ul>
</div>

Then I tried this:

var paneWrap = document.createElement("div");
$(".tab-pane > h5:nth-child(1)").append(paneWrap);

However in this case inside the h5 is the new div. Any ideas? tks!

Pranav Rustagi

Here is what is happening :

var paneWrap = document.createElement("div");
$(".tab-pane:nth-child(2)").append(paneWrap);

When you use the selector .tab-pane:nth-child(2), it selects all the .tab-pane elements which are second child. And after finding those elements, the div you created using paneWrap gets appended.

But when you use .tab-pane > h5:nth-child(1) for selector, it selects the h5 elements and appends the div inside it.

Now, to insert an element after/ before an element, here are the approaches :

var paneWrap = document.createElement("div");  //creating div element

For inserting after h5 :

$(".tab-pane > h5:nth-child(1)").after(paneWrap);

or

$(paneWrap).insertAfter(".tab-pane > h5:nth-child(1)");

For inserting before p tag :

$(".tab-pane > p:nth-child(2)").before(paneWrap);

or

$(paneWrap).insertBefore(".tab-pane > p:nth-child(2)");

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to append a letter to every element in an Array in scala

From Dev

Find first element with class, use class to append header element

From Dev

Javascript how to append an element to div with specific class

From Dev

Ruler append element every 5 pixels to fill it's width

From Dev

Append overwrites every element with last appended value (Python)

From Dev

How to use function on every element by class name?

From Dev

jquery (or css) every second element with class

From Dev

Fire JS code for every element with the same class

From Dev

JQuery check if existing appending element in element if not append to class but not this

From Dev

Append css to a class using a different class on the same element

From Dev

How to clone an element using class name and append it to the body

From Dev

Append a dom element on ng-click to a specific class

From Dev

jQuery append html element but with a different class name each time

From Dev

How to append HTML only to the current element slected by class

From Dev

Append a dom element on ng-click to a specific class

From Dev

How to append a class if element contains certain CSS property

From Dev

Loop through each element with class and append its text JQUERY

From Dev

Recreate a text effect by adding a class to every text element

From Dev

How to create a class after every single element dragged and dropped?

From Dev

Give every first, second and third element a unique class using jQuery

From Dev

javascript using a loop to change the display of every element that has a specific class

From Dev

add class to first 5 element from every 10

From Dev

Find every first instance of the first dynamic class of element with jquery

From Dev

How to apply a bootstrap class to every same HTML element in css file?

From Dev

Change color of every div element with the same class if another element is equals some text

From Dev

Add class to every 3rd element and exclude elements with X class

From Dev

Add class to every 3rd element and exclude elements with X class

From Dev

addClass append every time in every Clone

From Dev

Append a "+" at the beginning of every field without a "+"

Related Related

  1. 1

    How to append a letter to every element in an Array in scala

  2. 2

    Find first element with class, use class to append header element

  3. 3

    Javascript how to append an element to div with specific class

  4. 4

    Ruler append element every 5 pixels to fill it's width

  5. 5

    Append overwrites every element with last appended value (Python)

  6. 6

    How to use function on every element by class name?

  7. 7

    jquery (or css) every second element with class

  8. 8

    Fire JS code for every element with the same class

  9. 9

    JQuery check if existing appending element in element if not append to class but not this

  10. 10

    Append css to a class using a different class on the same element

  11. 11

    How to clone an element using class name and append it to the body

  12. 12

    Append a dom element on ng-click to a specific class

  13. 13

    jQuery append html element but with a different class name each time

  14. 14

    How to append HTML only to the current element slected by class

  15. 15

    Append a dom element on ng-click to a specific class

  16. 16

    How to append a class if element contains certain CSS property

  17. 17

    Loop through each element with class and append its text JQUERY

  18. 18

    Recreate a text effect by adding a class to every text element

  19. 19

    How to create a class after every single element dragged and dropped?

  20. 20

    Give every first, second and third element a unique class using jQuery

  21. 21

    javascript using a loop to change the display of every element that has a specific class

  22. 22

    add class to first 5 element from every 10

  23. 23

    Find every first instance of the first dynamic class of element with jquery

  24. 24

    How to apply a bootstrap class to every same HTML element in css file?

  25. 25

    Change color of every div element with the same class if another element is equals some text

  26. 26

    Add class to every 3rd element and exclude elements with X class

  27. 27

    Add class to every 3rd element and exclude elements with X class

  28. 28

    addClass append every time in every Clone

  29. 29

    Append a "+" at the beginning of every field without a "+"

HotTag

Archive