0

нужно чтобы li в строку было не больше 4 штук, после превышение лимита, новый li вставлялся во второй ряд. рядов не больше двух, width = 300px

var arr = [];
var arrList = "";
isHobbies = function() {
  var str = document.getElementById('hobbies');
  var theStr = str.value;
  arr.push(theStr);
  document.getElementById("str").innerHTML = "";
  for (var i = 0; i < arr.length; i++) {
      arrList = "<li>" + arr[i] + "</li>";
      document.getElementById("str").innerHTML += arrList;
  }
}
.interes {
  display: flex;
}
  <div class="hob">
  <label for="hobbies">Интересы</label>
  <input type="text" id="hobbies" placeholder="Ваш интерес?" name="" value="">
  <input type="button" id="addHobbies" name="" value="Добавить интерес" onclick="isHobbies()">
  <div id="str" class="interes">
  </div>
  </div>

2 ответа 2

0

Так?

const input = document.getElementById('input');
const list = document.getElementById('list');
const items = [];
const maxItems = 8;

addItem = () => {
  const value = input.value;

  if (value) {
    const item = document.createElement('li');

    item.className = 'item';
    item.innerHTML = value;

    if (items.length < maxItems) items.push(item);
    else {

    }

    list.innerHTML = '';

    for (const el of items) {
      list.innerHTML += el.outerHTML;
    }
  }
}
#list {
  border: 1px solid black;
  box-sizing: border-box;
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  list-style: none;
  margin: 10px 0 0;
  padding: 5px 7.5px 10px;
  width: 300px;
}

#list>.item {
  border: 1px solid black;
  flex-grow: 1;
  margin: 5px 2.5px 0;
  overflow: auto;
  padding: 5px;
  width: calc(25% - 20px);
}
<input id="input" type="text" />
<button type="button" onclick="addItem()">Button</button>
<ul id="list"></ul>

Под Ваш JS:

var arr = [];
var arrList = "";
isHobbies = function() {
  var str = document.getElementById('hobbies');
  var theStr = str.value;
  if (arr.length < 8) arr.push(theStr);
  document.getElementById("str").innerHTML = "";
  for (var i = 0; i < arr.length; i++) {
    arrList = "<li>" + arr[i] + "</li>";
    document.getElementById("str").innerHTML += arrList;
  }
}
#str {
  border: 1px solid black;
  box-sizing: border-box;
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  list-style: none;
  margin: 10px 0 0;
  padding: 5px 7.5px 10px;
  width: 300px;
}

#str>li {
  border: 1px solid black;
  flex-grow: 1;
  margin: 5px 2.5px 0;
  overflow: auto;
  padding: 5px;
  width: calc(25% - 20px);
}
<input id="hobbies" type="text" />
<button type="button" onclick="isHobbies()">Button</button>
<ul id="str"></ul>

2
  • правда я думал, что можно решить вопрос не меняя js)
    – Bender
    8 фев 2019 в 15:03
  • Поправил под Ваш JS (см. второй код). Но всё равно пришлось добавить лимит на 8 элементов.
    – Kvilios
    8 фев 2019 в 15:12
0

Ох уж эти js-ники... ) Вот вам вариант на css.

var arr = [];
var arrList = "";
isHobbies = function() {
  var str = document.getElementById('hobbies');
  var theStr = str.value;
  arr.push(theStr);
  document.getElementById("str").innerHTML = "";
  for (var i = 0; i < arr.length; i++) {
      arrList = "<li>" + arr[i] + "</li>";
      document.getElementById("str").innerHTML += arrList;
  }
}
.interes {
  column-count: 4;
  overflow: hidden;
  width: 300px;
  max-height: 2.4em;
  line-height: 1.2;
}
<div class="hob">
  <label for="hobbies">Интересы</label>
  <input type="text" id="hobbies" placeholder="Ваш интерес?" name="" value="">
  <input type="button" id="addHobbies" name="" value="Добавить интерес" onclick="isHobbies()">
  <div id="str" class="interes">
  </div>
  </div>

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.