JQuery - How to move a tag element with text inside to the nearest div

I have the following HTML structure:

<span class="green_box">
    <div class="icheckbox_flat-red_green" style="position: relative;">
        <input type="radio" name="minimal-radio" data-cid="5" data-uid="10" data-sid="1" data-pid="1" value="1" style="position: absolute; opacity: 0;">
        <ins class="iCheck-helper" style="position: absolute; top: 0%; left: 0%; display: block; width: 100%; height: 100%; margin: 0px; padding: 0px; border: 0px; opacity: 0; background: rgb(255, 255, 255);"></ins>
    </div>
    <i class="">1</i>
</span>
<span class="green_box">
    <div class="icheckbox_flat-red_green" style="position: relative;">
        <input type="radio" name="minimal-radio" data-cid="5" data-uid="10" data-sid="1" data-pid="1" value="1" style="position: absolute; opacity: 0;">
        <ins class="iCheck-helper" style="position: absolute; top: 0%; left: 0%; display: block; width: 100%; height: 100%; margin: 0px; padding: 0px; border: 0px; opacity: 0; background: rgb(255, 255, 255);"></ins>
    </div>
    <i class="">2</i>
</span>
<span class="green_box">
    <div class="icheckbox_flat-red_green" style="position: relative;">
        <input type="radio" name="minimal-radio" data-cid="5" data-uid="10" data-sid="1" data-pid="1" value="1" style="position: absolute; opacity: 0;">
        <ins class="iCheck-helper" style="position: absolute; top: 0%; left: 0%; display: block; width: 100%; height: 100%; margin: 0px; padding: 0px; border: 0px; opacity: 0; background: rgb(255, 255, 255);"></ins>
    </div>
    <i class="">3</i>
</span>

Now I want to move the tag <i>..</i>with the value inside the tag <div>..</div>, for example:

<span class="green_box">
    <div class="icheckbox_flat-red_green" style="position: relative;">
        <input type="radio" name="minimal-radio" data-cid="5" data-uid="10" data-sid="1" data-pid="1" value="1" style="position: absolute; opacity: 0;">
        <ins class="iCheck-helper" style="position: absolute; top: 0%; left: 0%; display: block; width: 100%; height: 100%; margin: 0px; padding: 0px; border: 0px; opacity: 0; background: rgb(255, 255, 255);"></ins>
        <i class="">1</i>
    </div>    
</span>
<span class="green_box">
    <div class="icheckbox_flat-red_green" style="position: relative;">
        <input type="radio" name="minimal-radio" data-cid="5" data-uid="10" data-sid="1" data-pid="1" value="1" style="position: absolute; opacity: 0;">
        <ins class="iCheck-helper" style="position: absolute; top: 0%; left: 0%; display: block; width: 100%; height: 100%; margin: 0px; padding: 0px; border: 0px; opacity: 0; background: rgb(255, 255, 255);"></ins>
        <i class="">2</i>
    </div>    
</span>
<span class="green_box">
    <div class="icheckbox_flat-red_green" style="position: relative;">
        <input type="radio" name="minimal-radio" data-cid="5" data-uid="10" data-sid="1" data-pid="1" value="1" style="position: absolute; opacity: 0;">
        <ins class="iCheck-helper" style="position: absolute; top: 0%; left: 0%; display: block; width: 100%; height: 100%; margin: 0px; padding: 0px; border: 0px; opacity: 0; background: rgb(255, 255, 255);"></ins>
        <i class="">3</i>
    </div>
</span>

I tried with jQuery like:

$('.green_box i').appendTo('.green_box div');

but does not work. Any idea how to move the tag <i>..</i>with the closest element <div>..</div>.

My JSFiddle: https://jsfiddle.net/47htz1yb/

thank

+4
source share
4 answers

You can use the method to do this work each().

$(".green_box").each(function(){
    $(this).find(".icheckbox_flat-red_green").append($(this).find("i"));
});
.icheckbox_flat-red_green > i {
    color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span class="green_box">
    <div class="icheckbox_flat-red_green">
        <input type="radio" name="minimal-radio">
        <ins class="iCheck-helper"></ins>
    </div>
    <i class="">1</i>
</span>
<span class="green_box">
    <div class="icheckbox_flat-red_green">
        <input type="radio" name="minimal-radio">
        <ins class="iCheck-helper"></ins>
    </div>
    <i class="">2</i>
</span>
<span class="green_box">
    <div class="icheckbox_flat-red_green">
        <input type="radio" name="minimal-radio">
        <ins class="iCheck-helper"></ins>
    </div>
    <i class="">3</i>
</span>
Run code

Using the above CSS, each element iin the class icheckbox_flat-red_greengets a color red.

+3
source

: https://jsfiddle.net/47htz1yb/3/

$('.green_box i').each(function(index, icon) {
    $(icon).siblings('div').append(icon)
})

$('.green_box i').appendTo('.green_box div'); , i .green_box div.

i, sibling div i.

+1

Try the following:

$('.green_box i').each(function(){ 
    var $this = $(this),
        $prev = $this.prev(),
        $i = $this.remove();

    $prev.append($i);
});
+1
source
$('.green_box i').each(function (i) {
    $('.green_box div').eq(i).append($(this));
});
0
source

All Articles