Select Parent DIV if i dont know the id?

Hello, I have a question:
I create a div, but my “platform” after executes the javascript, it creates a general div for all the elements that have been created using javascript. And it sets by default width and height(400, 200).

My div has 400 width, but I need auto height because I need to expand it automatically if I add new elements.

This is how looks on inspector:
https://puu.sh/B4zcC/3b9f90e19f.png

The problem is everytime the page loads, that div class number changes, so I can’t refer to it. My div is appended to auto-generated by default so…

Is there any way to select the parent of my div id= upholder?
And then modify the height to auto of the auto-generated div.

Actually, when I add items to my div, it looks like cut, because the “parent” div has 200 height.

Thanks!!

You can get the parent node via the property of that name:

document.getElementById('upholder').parentNode.style.height = 'auto'

// Or just remove the height inline style to fall back to the CSS:
document.getElementById('upholder').parentNode.style.height = ''

But if that div gets created dynamically with JS it may not be available from the start, so you’ll have to find a way to wait for that element to be appended to the document.

1 Like

Worked like a charm!!
Awesome!

Thanks :heart:

1 Like

Glad I could help! :-)

I was going to mention querySelector

Does it make any difference using getElementById over querySelector, or vice versa?

Barry

No, not really… getElementById() is slightly faster, but that shouldn’t matter in any real-world application. Personally I just like to use getElementById() to express more clearly when I’m interested in that unique element, rather than the first element that happens to match the selector.

2 Likes

When I use getElementById I feel more comfortable because I know I’m referring only to that element :rofl:
querySelector feels like “70-80%” accurate.

1 Like

LOL but no I think you’re confusing this with the quantumSelector(), which simultaneously both does and does not find a match until its result gets accessed. :-P

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.