Solved

How to disect a chunk of HTML and maintain the data

Posted on 2014-09-12
3
335 Views
Last Modified: 2014-10-01
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

0
Comment
Question by:Al4ddin2
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 250 total points
ID: 40319889
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
 
LVL 43

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 250 total points
ID: 40319895
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
 
LVL 43

Expert Comment

by:Rob
ID: 40320308
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

Featured Post

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question