Generate CSS selector for the target DOM element | JavaScript Interview Question - 24

preview_player
Показать описание
JavaScript Interview Question - 24 | In this video, we will see how to solve a medium-difficulty problem asked frontend engineer interview to SDE2 and SDE3.

Given a target DOM element and the root element, create a function to generate the CSS selector from the root to the target.

Loved the question?, I have 120+ such solved problems in my ebook "JavaScript Interview Guide". If you are preparing for Interviews and looking for a solutions book, Get my Ebook.

Social links
Рекомендации по теме
Комментарии
Автор

Thankyou for increasing the audio of the video 🙏🙏👏

vanshsharma
Автор

Great Explanation 🙏🏻🤩
Please bring more of the DOM traversal questions like this one.

dukwiam
Автор

Hi Prashant, solved it on my own.

//Below is my code
const generateSelector = (root, target) => {
const selector = []
let child = target
let parent = null

while(parent !== root){
parent = child.parentNode

child = parent
}

const parentSelector =
selector.push(parentSelector)

selector.reverse()
return selector.join(' > ')
}

let rootElm =
let targetElm =

console.log(generateSelector(rootElm, targetElm))

akash-kumar
Автор

I just revised the DOM manipulation yesterday and watching your video today just makes it clear in my head.

Nice one.

Keep coming up with more such videos. 👏

chahatbhatia
visit shbcf.ru