Discovering a child element using the reference of its parent: A guide

I'm trying to figure out how to access items using vue's this.$refs without relying on jQuery. Here is the setup I have:

<template>
  <div>
    <svg ref="svgItem">
      <path d="M899.33,118.33h-81.7v90.2h-31.3V2.07h31.3v88.5h81.7V2.07h31.29V208.53H899.33Z" />
    </svg>
  </div>
</template>

<script>
import { TimelineLite } from 'gsap';

export default {
  mounted() {
    const { svgItem } = this.$refs;
    const timeline = new TimelineLite();
    timeline.to(svgItem, 5, { opacity: 0.3 });
    // timeline.to('${svgItem} > path', 5, { opacity: 0.3 }); // something like this :)
  },
};
</script>

In the past, I would achieve this with jQuery like so:

const svgPath = $('#parent path');
// or maybe:
const svgPath = $('#parent').find('path');

Now, I am considering using querySelector but unsure how to dynamically mix variables and strings together:

const { svgItem } = this.$refs;
const selector = document.querySelector(svgItem > 'path')  // something like this :)

const timeline = new TimelineLite();
timeline.to(selector, 5, { opacity: 0.3 });

If you could provide some guidance, it would be greatly appreciated.

Answer №1

The querySelector method is not limited to the document object; it can also be used on HTML Element.

const { circleElement } = this.$refs;
const circlePath = circleElement.querySelector('path');

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Trouble getting CSS to load in Webpack

I'm having some trouble setting up Webpack for the first time and I think I might be overlooking something. My goal is to use Webpack's ExtractTextPlugin to generate a CSS file in the "dist" folder, but it seems that Webpack isn't recognizi ...

The challenge of incorporating Laravel, Vue, and JavaScript into a Blade template

It may seem like a silly question, but I am struggling to find a solution. My goal is to load a Vue component and JS file into a blade view. When I include the following: <script src="{{ asset('js/app.js') }}"></script> <script sr ...

Canceling ongoing Angular HTTP request does not stop

Is there a way to properly cancel an $https request in Angular when a new request is triggered? Despite using allMarkersRequest.abort(), the pending request still shows up in Chrome's dev tool. What am I missing? In the code snippet below, allMarkers ...

Exploring the occurrence of immutation within nested arrays inside objects

let myObject = {key : 'Add', array : [0, 2, 3]} let clonedObject = Object.assign({}, myObject, {array: myObject.array}) clonedObject.array.pop() console.log(myObject) console.log(clonedObject) In the above example, let's say we want to rem ...

Why does AngularJS $watch only execute once?

Why do the codes in the watch only run once? How can I address this issue? this.$rootScope.$watch('tabType', () => { if (this.$rootScope["tabType"] === TabType.Sent) { this.$scope.refreshSentList(); } else if (this.$rootScope[ ...

Error in vue.js: Cannot access property '$forContext' of null object

I have come across a peculiar issue. Despite everything appearing to function correctly, when I submit inputs (whether using the form submit event with a <form/> element or the keyop.enter event), I encounter the following error in my JS console that ...

Error: 'vue' command not found - encountered while creating a vite application

I recently started a new Vue 3 project using Vite, and I wanted to incorporate Vuetify despite it being in alpha stage. Following the tutorials, I tried using "vue add vuetify," but all I received was a "command not found error." What could be going wrong? ...

Can we access an element within the document using its context?

One common method to access an element is by using its id: <div id="myId"></div> <script type="text/javascript"> $(document).ready(function(){ $('#myId').something(); }); </script> However, there are inst ...

How can JavaScript be used to rearrange the placement of DOM elements?

<!DOCTYPE html> <html> <head> <title></title> </head> <body> <div class="boxes"> <div class="red" style="width: 300px; height: 300px; color: red"></div> <div class="blue ...

What is the best way to conceal the standard 'X' close button within a magnific popup interface?

I implemented the Magnific-popup plugin in my jsp to show messages to users. Here is the code I used to open Magnific Popup: $.magnificPopup.open({ items: { type: 'inline', src: '#idOfSomeDivInPage' }, focus: '#some ...

What is the best way to locate all mesh faces that are being lit up by a SpotLight

I am working with a THREE.Mesh that consists of a THREE.BufferGeometry containing "position" and "normal" THREE.BufferAttributes. This mesh is being lit by a THREE.SpotLight (which is a cone-shaped light source). Is there a method to ...

Having trouble decoding the downloaded font in VueJS with Bootstrap icons - the sfntVersion appears to be invalid

Bootstrap-icons are causing issues in my VueJS application. Upon loading the page, I encounter these errors: Failed to decode downloaded font OTS parsing error: invalid sfntVersion: 1008813135 specifically for the .woff and .woff2 fonts within bootst ...

What is the best way to pass a bind value in an Angular function controller?

I am new to working with AngularJS and I'm trying to pass a model bind value into a function declared in the controller. However, when I try to access that value from the controller, it returns undefined. Here is the code snippet: HTML: <div> ...

Guide on implementing tail.select in a VueJS project

As a newcomer to VueJS, I am facing issues with using the tail.select plugin in my VueJS project. Even though I have imported the plugin in my main.js file using import 'tail.select' When I try to call tail.select(".select") in the mounted hook ...

Create a named scopedSlot dynamically

Looking to incorporate the following template into my component's render function, but struggling with how to do so. This is the current template structure: <template> <div> <slot name="item" v-for="item in filte ...

Duplicate a Google Sheet and save it to a specific folder in Google Drive

I currently have two spreadsheets in my possession. The first spreadsheet consists of raw data that includes unique employee numbers and the names of the employees. The second spreadsheet is the one I aim to duplicate to a designated Google Drive folder. M ...

Initialize the Vuex state upon app startup according to the route parameters

For my Vue app, I have numerous routes that contain a 'tenantId' parameter. Upon the initial load of the app, I need to extract the 'tenantId' value from the route, fetch data from an API, and set up the Vuex store accordingly. Since t ...

Learn how to make a POST request using Fetch in Vuejs2

I am working on a form that includes a text field. When the button is clicked, I need to send a request with the value entered in the input field. Any suggestions on how to achieve this? <div class=" select__body" v-if="addedForm === 3 ...

Transmitting the character symbol '&' within a string from Angular to PHP

My goal is to transmit a string, such as "the cat & the dog", from Angular to PHP using GET method. I have used encodeURI(note) in Angular and in PHP $note = $_GET['note']; $note = mysql_real_escape_string($note); However, when it gets inse ...

Increment numbers using XML elements

In my XML construction process, I start with a base XML and add elements like this: $(xml).find('PARENT').find('CHILDREN').each(function(i){ outputstr += '<lorem id="">' }) As the "parent" object appears mul ...