简体   繁体   中英

Shift data of one li & ul tag from one div class tag to another div id tag without any li & ul tags just only data

This is my code below:

<div id="title">
      <span><b>Title</b></span>
</div>

<div class="data">
    <ul>
            <li id="cp_name" class=""><span>Name:</span> XXXX</li>
            <li id="cp_dept" class=""><span>Dept:</span> Editing</li>
            <li id="cp_role" class=""><span>Role:</span> YYYY</li>
    </ul>
</div>

Original Output

Title

  • Name: XXXX
  • Dept: Editing
  • Role: YYYY

Expected Output & Code

Title

Dept: Editing

  • Name: XXXX
  • Role: YYYY

     <div id="title"> <span><b>Title</b></span> <div id="cp_dept"> <span>Dept:</span> Editing </div> </div> <div class="data"> <ul> <li id="cp_name" class=""><span>Name:</span> XXXX</li> <li id="cp_role" class=""><span>Role:</span> YYYY</li> </ul> </div> 

I want to extract or just remove li & ul tags of "cp_dept" ie Dept: Editing data from div class="data" & wan't to shift data into div id="title" tag without li & ul tags ie just only data as shown in Expected Output & Code that too with immediate effect on page loading.

Demo

Try this code..

var li=$('.data>ul>li').eq(1).remove();
var id=li.attr('id');
var html=li.html();
var div1=$('<div></div>').attr('id',id).append(html);
$('#title').append(div1);
var dept = $(".data ul li#cp_dept").html(); dept = "<div id='cp_dept'>" + dept + "</div>"; $("#title").append(dept); $(".data ul li#cp_dept").remove();

Use insertAfter jQuery function : (remove and append)

$(function() {
    $('#cp_dept').insertAfter('#title span');
});

Try

$('#cp_dept').detach().wrapInner('<div />').contents().unwrap().appendTo('#title');

Demo: Fiddle

Check out this javascript only answer

JS

(function() {
     var dpt = document.getElementById('cp_dept');
     dpt.outerHTML=""
     var tit = document.getElementById('title');
     tit.innerHTML = tit.innerHTML +"<br>"+dpt.textContent;
  })();

Working Fiddle

Just added or edited a line in the Javascript. Instead of removing or editing cp_dept, I assign the CSS to display:none . It does the same function but this way I'm not erasing the contents of the cp_dept tag and hence the HTML markup remains intact.

(function() {
     var dpt = document.getElementById('cp_dept');
    // dpt.outerHTML=""
     dpt.style.display="none";
     var tit = document.getElementById('title');
     tit.innerHTML = tit.innerHTML +"<br>"+dpt.textContent;
  })();

Try this

var htm = $("#cp_dept").text();
$("#data ul #cp_dept").remove();
$("#title").append('<div id="cp_dept">'+htm+'</div>');

Here is the code

$(document).ready(function() {
    var getli = $('.data #cp_dept');
    $('.data #cp_dept').remove();    
    $creDiv = $('<div />').attr('id',getli.attr('id'))
                          .html(getli.html());
    $("#title").append($creDiv);
});

DEMO

But you may be looking for more, please comment on it

You can create a new element to append the "cp_dept" target, then add the new element to "#title".

var title = document.getElementById('title');
var dept = document.getElementById('cp_dept');

var newDept = document.createElement('div');
newDept.innerHTML = dept.innerHTML;

// remove the old cp_dept
dept.parentNode.removeChild(dept);

title.appendChild(newDept);
$("li#cp_dept").each(function(){

var div = $("<div id='"+$(this).attr("id")+"'/>");

div.html($(this).html());

div.appendTo("div#title");

$(this).remove();

});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM