Link to home
Start Free TrialLog in
Avatar of Al4ddin2
Al4ddin2

asked on

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

ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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