Solved

How to disect a chunk of HTML and maintain the data

Posted on 2014-09-12
3
322 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
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
Find out what you should include to make the best professional email signature for your organization.
In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…

808 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