Javascript-форум (https://javascript.ru/forum/)
-   Events/DOM/Window (https://javascript.ru/forum/events/)
-   -   Не могу всавить строку в элемент (https://javascript.ru/forum/events/85581-ne-mogu-vsavit-stroku-v-ehlement.html)

ureech 31.10.2023 17:28

Не могу всавить строку в элемент
 
var sibling = document.querySelector('[data-first="1"]')

console.log(sibling )
// <div class="day col slot colorRow" data-itemid="1" data-date="1699077600000" style="background: rgb(250, 235…: 0px; border-top: 0px;" data-children="" data-first="1" onclick="setSlot(this)">

console.log(sibling.children)
// 
HTMLCollection { 0: span.price-hover.first, length: 1 }
length: 0
<prototype>: HTMLCollectionPrototype { item: item(), namedItem: namedItem(), length: Getter, … }

console.log(sibling.children[0])
// <span class="price-hover first">

console.log(sibling.children[0].innerHTML)
// 600


Как вставить в sibling.innerHTML строку?
Всё это получаю при клике на соседний элемент

ureech 31.10.2023 18:06

Вроде разобрался. Сделал так
var arr = Array.from(sibling.children)
arr[0].innerHTML = '...'
sibling.appendChild(arr)

Правда ошибка вылазиет
Uncaught TypeError: Node.appendChild: Argument 1 does not implement interface Node.


Но если так
sibling.appendChild(arr[0])
, то не работает

ureech 31.10.2023 19:20

Ошибка в коде была. Решено)

voraa 31.10.2023 21:31

Что куда вставлять так и не ясно.
В <span class="price-hover first">?
Перед <span class="price-hover first">?


Может с помощью insertAdjacentHTML надо было вставлять?

ruslan_mart 31.10.2023 23:34

sibling.firstElementChild.append(document.createTextNode('some text'));

voraa 01.11.2023 07:34

Цитата:

Сообщение от ruslan_mart
sibling.firstElementChild.append(document.createTe xtNode('some text'));

createTextNode - не обязательно. Можно просто
sibling.firstElementChild.append('some text')


Часовой пояс GMT +3, время: 02:22.