Search code examples
javascriptmutation-observers

How can I get a tag type with a MutationObserver?


        let mObserver = new MutationObserver((mutationsList, observer) => {
            mutationsList.forEach((mutation) => {
                if (mutation.type === 'childList' && mutation.addedNodes.length > 0) {
                    mutation.addedNodes.forEach((node) => {
                        console.log(node)
                    })
                }
            })
        })

        mObserver.observe(document.body, {
            childList: true,
            subtree: true
        })

How can I tell the type of node that was added? I want to target only a tags


Solution

  • You can use nodeName

    if (node.nodeName.toLowerCase() === 'a') {
        // Do things
    }