Monday, 15 February 2010

html - jquery - Looking for same content -



html - jquery - Looking for same content -

i've trying getting working selectors, while, for, etc i'm not getting close in want.

<div class="product"> <div class="id">569865974598</div> <div class="name">ati radeon sapphire hd 6850</div> <div class="time">2013-02-14 03:13:33</div> </div> <div class="product"> <div class="id">654654654987</div> <div class="name">nvidia geforce 9500gt</div> <div class="time">2013-02-14 00:36:13</div> </div> <div class="product"> <div class="id">2561564898789</div> <div class="name">ati radeon sapphire hd 6850</div> <div class="time">2013-02-14 00:36:13</div> </div>

expected(encapsulate products same time)

<div id="same"> <div class="product"> <div class="id">569865974598</div> <div class="name">ati radeon sapphire hd 6850</div> <div class="time">2013-02-14 03:13:33</div> </div> </div> <div id="same"> <div class="product"> <div class="id">654654654987</div> <div class="name">nvidia geforce 9500gt</div> <div class="time">2013-02-14 00:36:13</div> </div> <div class="product"> <div class="id">2561564898789</div> <div class="name">ati radeon sapphire hd 6850</div> <div class="time">2013-02-14 00:36:13</div> </div> </div>

try this:

$(document).ready(function(){ $('.product').each(function(){ // generate div id based on time e.g. 2013-02-14 00:36:13 -> id=20130214003613 var id = $(this).find('.time').html().replace(/[\s-:]/g,""); var sameelement = $('#' + id); if(sameelement.length == 0){ $('body').append('<div id=' + id + '></div>'); sameelement = $('#' + id); } $(this).appendto(sameelement); }); });

note: having multiple div elements same id not correct, why instead of proposed id='same' there generated id based on time

fiddle: http://jsfiddle.net/zt38s/

jquery html select addclass

No comments:

Post a Comment