K
K
KGZVER2021-08-26 18:05:28
React
KGZVER, 2021-08-26 18:05:28

How to find out the number of a div element inside a div?

For example, there is such a JSX element, you need to number each 'item-title' according to the hierarchy of its location inside the main div. Now I wrote it manually, but this is not the correct option, because the view depends on the background.

<div className='settings' id='settings'>
            <div className='settings_item'>
                <h3 className='item-title'>1. Почта </h3>
                <div className='item_content'>
                    ..content
                </div>
            </div>
           <div className='settings_item'>
                <h3 className='item-title'>2.Телефон </h3>
                <div className='item_content'>
                    ..content
                </div>
            </div>
</div>


I tried this, but the code doesn't work.

const settingContent = document.getElementById('settings')

    useEffect(() => {
        settingContent?.childNodes.forEach((item, key) => {
            const div = document.createElement('div')
            div.innerHTML = `${key}`
            console.log(item.childNodes.item(0))
            item.childNodes.item(0).appendChild(div)
        } )
    }, [])

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
andand44, 2021-08-26
@KGZVER

Can be done without js, only with styles using pseudo-elements
https://jsfiddle.net/oygt1c8s/1/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question