Solved

Find function skips duplicates

Posted on 2010-08-20
3
284 Views
Last Modified: 2012-05-10
Hi,

I have a div like this:

 <div id="hintdata"><div class='10003137'>Key Skills Level 3</div><div class='10003162'>Key Skills Level 3</div><div class='10003198'>Key Skills Level 3</div><div class='10030463'>Hairdressing</div><div class='10030475'>Hairdressing</div><div class='10038310'>Key Skills Level 3</div><div class='10038358'>Key Skills Level 3</div><div class='10038395'>Key Skills Level 3</div><div class='10058722'>Food level 2</div><div class='10064588'>Additional Maths</div><div class='10064588'>Additional Maths test100510</div><div class='50018048'>Welsh bacc Level 3</div><div class='50018073'>Art and Design</div><div class='50018097'>Art and Design</div><div class='50018103'>Art and Design</div><div class='50020481'>Food level 2</div><div class='50036725'>Travel and Tourism Year 12</div><div class='50036725'>Travel and Tourism Year 13</div><div class='50042555'>History of Art</div><div class='50067540'>Food Level 1</div></div></div>

Open in new window


and when I run this in firebug: $('#hintdata').find('div').each(function(index,element){
console.log($(element).text());
})

it does not return the div's with the same number.:

[div.10003137, div.10003162, div.10003198, div.10030463, div.10030475, div.10038310, div.10038358, div.10038395, div.10058722, div.50018048, div.50018073, div.50018097, div.50018103, div.50020481, div.50042555, div.50067540]

Open in new window



I would like to aggregate each div so that for example :

<div class='10064588'>Additional Maths</div> <div class='10064588'>Additional Maths test100510</div>

is edited to:
[code]
<div class='10064588'>Additional Maths<br />Additional Maths test100510</div> 

Open in new window

How would this be done?

Darren
0
Comment
Question by:darren-w-
  • 2
3 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 33484387
Check this :


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script language="javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"></script>
<script language="javascript">
	$(document).ready(function() {
		var classes = [];
		$("div","#hintdata").each(function() {
			var c = $(this).attr("className");
			if(classes[c]) {
				$("div."+c).append("<br />"+ $(this).html());
				$(this).remove();
			}
			else {
				classes[c] = true;
			}
		});
                // checking
		//alert( $(".10064586").html() );
	});
</script>
</head>
<body>
<div id="hintdata">
<div class='10064588'>Additional Maths1</div><div class='10064588'>Additional Maths1 test1</div>
<div class='10064587'>Additional Maths2</div><div class='10064587'>Additional Maths2 test2</div>
<div class='10064586'>Additional Maths3</div><div class='10064586'>Additional Maths3 test3</div>
<div class='10064585'>Additional Maths4</div><div class='10064585'>Additional Maths4 test4</div>
<div class='10064584'>Additional Maths5</div><div class='10064584'>Additional Maths5 test5</div>
</div>
</body>
</html>

Open in new window

0
 
LVL 13

Author Closing Comment

by:darren-w-
ID: 33484779
That worked great, thanks Darren
0
 
LVL 82

Expert Comment

by:leakim971
ID: 33484792
You're welcome! Thanks for the points!
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

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 …
Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
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)
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

911 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

23 Experts available now in Live!

Get 1:1 Help Now