Create element among child elements of the same Father

Asked

Viewed 52 times

1

According to the code below:

var ul = document.getElementsByTagName('ul');
var ref = ul.item(0).children;
var saida = '';

  for (i = 0; i <= ref.length; i++) {
if (i == 0) {
	var HTMLString = "<div style='background-color: blue; width: 60px; height: 60px;'>";
    var div = document.createElement('div');
    div.innerHTML = HTMLString;
    ref[i].parentElement.insertBefore(div.firstChild, ref[i]);
}
if (i == 11) {
	var HTMLString = "</div><div style='background-color: red; width: 60px; height: 60px;'>";
    var div = document.createElement('div');
    div.innerHTML = HTMLString;
    ref[i].parentElement.insertBefore(div.firstChild, ref[i]);
}
  }
<!DOCTYPE html>
<html>
<body>

<div id="div1">
  <ul>
  <li>1</li>
  <li>2</li>
  <li>3</li>
  <li>4</li>
  <li>5</li>
  <li>6</li>
  <li>7</li>
  <li>8</li>
  <li>9</li>
  <li>10</li>
  <li>11</li>
  <li>12</li>
  <li>13</li>
  <li>14</li>
  <li>15</li>
  <li>16</li>
  <li>17</li>
  <li>18</li>
  <li>19</li>
  <li>20</li>
  <li>21</li>
  <li>22</li>
  <li>23</li>
  <li>24</li>
  <li>25</li>
  </ul>
</div>

</body>
</html>

It would be possible to open the <div> before the first element <li> and close it after the 10th element <li> ??

If yes, how to do?


Obs.: According to the <script> that I created it creates the element <div> and closes in the same place ...
I used the concept of reply of this question.

  • 1

    Can you explain what you want to do as a final product? Breaking ul > li syntax with Divs doesn’t sound very good.

  • 1

    Well, at the time of printing the page that contains these elements, they end up being printed separately on the other page, by the amount generated, then I thought of dividing these elements in DIVs applying a float: left to organize them side by side, when they reach a certain amount, understands ?

  • Maybe it would be interesting if I appendChild in the items up to the desired amount for the DIV that I created and then remove the elements that were copied ?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.