E
E
Extramezz2015-10-15 14:51:32
css
Extramezz, 2015-10-15 14:51:32

Is it considered bad practice to use text nodes?

There is a markup:

<div><span class="font-ico">Тут иконка</span>1234566</div>

And there is a task - to change the number.
I can do like this:
document.querySelector("div").childNodes[1].data = 222222;

But what is interesting - is this considered normal practice or is it better to wrap the number in 1 more tag and refer to it?
document.querySelector(".chislo").textContent = 22222;

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Kovalsky, 2015-10-15
@dmitryKovalskiy

The span tag is made to logically highlight text. If you have a block that is changed by means of client scripts, it is logical to separate such elements into a separate tag. In my opinion, the problem is that today you have the opportunity, by indirect signs, to tear out this text, and tomorrow something will be added there and this opportunity will disappear and you still have to make a selection in the tag.

A
Alex K, 2015-10-15
@alexk111

it is better to wrap:
1. in which case it will be easier to look for pieces of code that work with a number.
2. when accessing not through childNodes, but through a selector, you will be insured against bugs due to the addition of any new elements inside

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question