javascript - How to divide result given in html() jquery? -
i'm trying create function click button , content <div>
. after, need divide content. mean, if div has 10 children, need save 5 children in var code1
, other 5 in var code2
. problem i'm not able use html() function. code looks like:
$(".pluscontrol").click(function(){ var id = $(this).attr("id"); var items=$(this).closest("table").parent().next().children('#'+id).children().length; var middle = items / 2; var code1=""; $(this).closest("table").parent().next().children('#'+id).html( function(index,currentcontent){ if (index < middle ) code1 = code1 + currentcontent; }); if ( $(".modal-body .row .sdiv").attr("id") == 1 ) $(".modal-body .row #1.sdiv").html(code1); if ( $(".modal-body .row .sdiv").attr("id") == 2 ) $(".modal-body .row #2.sdiv").html("..."); });
as can see, @ first, children lenght items.
i've checked this reference not helps much
the var items number of items
giving argument .html()
makes change html of selected elements. if want html, call .html()
no argument.
to html of elements in collection, use .map()
iterate, combine them .join()
.
var allchildren = $(".parent").children(); var middle = math.floor(allchildren.length/2); var code1 = allchildren.slice(0, middle).map(function(index, element) { return element.innerhtml + " " + index; }).get().join(' '); var code2 = allchildren.slice(middle).map(function(index, element) { return element.innerhtml + " " + (index + middle); }).get().join(' '); $("#out1").html(code1); $("#out2").html(code2);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div class="parent"> <div class="child">test1</div> <div class="child">test2</div> <div class="child">test3</div> <div class="child">test4</div> <div class="child">test5</div> <div class="child">test6</div> <div class="child">test7</div> <div class="child">test8</div> <div class="child">test9</div> <div class="child">test10</div> </div> first group: <div id="out1"></div> second group: <div id="out2"></div>
Comments
Post a Comment