Skip to content

useParentElement

Category
Export Size
467 B
Last Changed
6 months ago

Get parent element of the given element

Demo

Current element tag: ASIDE

Parent element tag: DIV

Usage

When no argument is passed, it will return the parent element of the current component.

vue
<script setup lang="ts">
import { 
useParentElement
} from '@vueuse/core'
const
parentEl
=
useParentElement
()
onMounted
(() => {
console
.
log
(
parentEl
.
value
)
}) </script>

It can also accept a ref as the first argument.

vue
<script setup lang="ts">
import { 
useParentElement
} from '@vueuse/core'
import {
shallowRef
} from 'vue'
const
tooltip
=
shallowRef
<HTMLElement | undefined>()
const
tooltipWrapper
=
useParentElement
(
tooltip
)
onMounted
(() => {
console
.
log
(
tooltipWrapper
.
value
)
}) </script> <template> <
div
>
<
p
ref
="
tooltip
" />
</
div
>
</template>

Type Declarations

ts
export declare function 
useParentElement
(
element
?:
MaybeRefOrGetter
<HTMLElement | SVGElement | null | undefined>,
):
Readonly
<
ShallowRef
<HTMLElement | SVGElement | null | undefined>>

Source

SourceDemoDocs

Contributors

Anthony Fu
IlyaL
Anthony Fu
青椒肉丝
Arthur Darkstone
JD Solanki

Changelog

v12.8.0 on
7432f - feat(types): deprecate MaybeRef and MaybeRefOrGetter in favor of Vue's native (#4636)
v12.3.0 on
59f75 - feat(toValue): deprecate toValue from @vueuse/shared in favor of Vue's native
v12.0.0-beta.1 on
0a9ed - feat!: drop Vue 2 support, optimize bundles and clean up (#4349)
v10.0.0-beta.4 on
4d757 - feat(types)!: rename MaybeComputedRef to MaybeRefOrGetter
0a72b - feat(toValue): rename resolveUnref to toValue
v10.0.0-beta.2 on
e8168 - feat: new function (#2855)

Released under the MIT License.

LIMITED OFFER
50% OFF Official Vue.js Developer Certification
Get Certified
03
hours
:
42
minutes
:
36
seconds
: