Solved

How to disect a chunk of HTML and maintain the data

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

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 42

Expert Comment

by:Rob Jurd, EE MVE
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now