How to disect a chunk of HTML and maintain the data

In my HTML I have a list.
The list contains items that the user can select and 'add' to their own container. So each list item has a button, when the button is clicked at the moment it clones the HTML and adds it into their container HTML div.
Is there a way that I can still capture all the data in that HTML snippet but not clone the HTML exactly? I would like it to appear differently.

<li><div class="item itemContainer" >
<div class="itemId">123</div>
<div class="itemTitle">Title 1</div>
<div class="itemContributor">Test Name</div>
</div>
<div class="item pageNum">6</div>
<div class="item itemSelection" ><div class="previewItem"><input type="button" value="Preview" class="previewBtn"></div>
<div class="addToMe"><input type="button" value="ADD >" id="addToMe" class="addToMeBtn"></div>
</div>
<div class="imagePreview hide"><img src="/image.gif"></div>
</li>

Open in new window



The for example I would like it to end up looking something like this:
<li class="">
                        <div class="item pageNum" title="">6</div>
                        <div class="item itemContainer" >123</div>
                            <div class="itemInfo">
                                <span class="itemTitle">Title 1</span>
                                <span class="itemContributor">Test Name</span>
                            </div>
                        </div>
                        <div class="item handle" title="">
                            <span class="ui-icon ui-icon-carat-2-n-s"></span>
                        </div>
                        <div class="item">
                            <input type="button" value="remove" />
                        </div>
                    </li>

Open in new window

Al4ddin2Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

leakim971PluritechnicianCommented:
You need to use multipl jQuery selector based on the ref of the main LI
test page : http://jsfiddle.net/ppxzyrc8/
var mainLI = $(this); // for example
var newLI = $("<li />");
$(".item.pageNum", mainLI).appendTo(newLI); // put it in the top of the main LI : <div class="item pageNum" title="">6</div>
$(".item.itemContainer").appendTo(newLI); // <div class="item itemContainer" >123</div>
$(".itemInfo").appendTo(newLI); // <div class="itemInfo">.... </div>
$(".item.handle").appendTo(newLI); // <div class="item handle" title="">..</div>
$(".item").appendTo(newLI); //  <div class="item">...</div>
mainLI.replaceWith(newLI);

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Chris StanyonWebDevCommented:
The basic idea here would be this. When the button is clicked, grab a reference to the LI, and then search that LI for the data you need in your new layout and build it from there:

Quick overview:

$('.addToMeBtn').click(function() {
	// get the parent LI
	item = $(this).parents('li');
	
	// search for the itemTitle class in item
	itemTitle = $('.itemTitle', item);
	
	// create a new LI
	newLI = $('<li>');
	
	// add the itemTitle to the new LI
	newLI.text(itemTitle.text());
	
	// add the new LI to another UL (with an ID of newList)
	$('#newList').append(newLI);
});

Open in new window

An issue that you also have is that you seem to be setting the id of your button to 'addToMe' - IDs need to be unique so you should either bin that or make sure it's different for every button.
0
RobOwner (Aidellio)Commented:
simple enough with a view model using knockoutjs, DEMO: http://jsbin.com/wevaz/1

CODE:
http://jsbin.com/wevaz/1/edit?html,js,output

javascript
var tmp = [
	new item({
		id: '123',
		title: 'title 1',
		contributor: 'test name',
		pagenum: 6,
		selection: 'none'
	}),
	new item({
		id: '456',
		title: 'title 2',
		contributor: 'this contributor',
		pagenum: 3,
		selection: 'none'
	})
];

$(function() {
	viewModel = {
		myoptions: ko.observableArray(tmp),
		myselections: ko.observableArray()
	};
	
	viewModel.selectMe = function(obj) {
		viewModel.myoptions.remove(obj);
		viewModel.myselections.push(obj);
	};
	
	viewModel.removeMe = function(obj) {
		viewModel.myselections.remove(obj);
		viewModel.myoptions.push(obj);		
	};
	
	ko.applyBindings(viewModel);
});

function item(data) {
	this.id = data.id;
	this.title = data.title;
	this.contributor = data.contributor;
	this.pagenum = data.pagenum;
	this.selection = data.selection;
	
}

Open in new window


html
<!DOCTYPE html>
<html>
	<head>
		<script src="//code.jquery.com/jquery.min.js"></script>
		<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
		<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
		<script src="//code.jquery.com/jquery-2.1.1.min.js"></script>
		<script src="//cdnjs.cloudflare.com/ajax/libs/knockout/3.0.0/knockout-min.js"></script>
		<meta charset="utf-8">
		<title>JS Bin</title>
	</head>
	<body>
		<ul data-bind="foreach: myoptions" style="float: left;width: 49%; border: 1px solid black;">
			<li>
				<div class="item itemContainer" >
					<div class="itemId" data-bind="text: id"></div>
					<div class="itemTitle" data-bind="text: title"></div>
					<div class="itemContributor" data-bind="text: contributor"></div>
				</div>
				<div class="item pageNum" data-bind="html: pagenum"></div>
				<div class="item itemSelection" ><div class="previewItem"><input type="button" value="Preview" class="previewBtn"></div>
					<div class="addToMe"><input type="button" value="ADD >" id="addToMe" class="addToMeBtn" data-bind="click: $root.selectMe"></div>
				</div>
				<div class="imagePreview hide"><img src="/image.gif"></div>
			</li>
		</ul>
		<!-- selections -->
		<ul data-bind="foreach: myselections" style="float: left;width: 49%; border: 1px solid black;">
			<li class="">
				<div class="item pageNum" title="" data-bind="text: pagenum"></div>
				<div class="item itemContainer" data-bind="text: id">123</div>
				<div class="itemInfo">
					<span class="itemTitle" data-bind="text: title"></span>
					<span class="itemContributor" data-bind="text: contributor"></span>
				</div>
			</div>
		<div class="item handle" title="">
			<span class="ui-icon ui-icon-carat-2-n-s"></span>
		</div>
		<div class="item">
			<input type="button" value="remove" data-bind="click: $root.removeMe"/>
		</div>
		</li>
	</ul>
</body>
</html>

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
jQuery

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.