What is the best way to access an HTML element when incorporating Vue through a CDN?

In my project, I am utilizing Vue 3.0.2 by loading it through CDN with the following code:

<head>
 ...
  <script src="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f7818292b7c4d9c7d9c5">[email protected]</a>"></script>
</head>

I am wondering how I can access an HTML element within my Vue component when importing the library via CDN. In plain JavaScript, I would use

document.getElementById('element')
.

Previous discussions on this platform suggest that using $this.el.querySelector should do the trick, but I seem to lack access to that function. Could this be due to importing Vue via CDN?

This is what my component structure looks like...

const app = Vue.createApp({
        data() {
            return {
                // data members.
            }
        },
        methods: {
            // method definitions.
        } });

app.mount('#app')

Appreciate any help and guidance!

Answer №1

Here is a different approach you could consider

  <script src="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="abcdefdfcaef8cdbbfdabeaeadaeafb7fcfdfcadafbedaebcafde6gceeebfdecbbffeeefaebfcecdffacfd">[email protected]</a>"></script>
  </head>
  <body>
    <div id="app">
      <h1>{{ newData }}</h1>
      <input type="text" ref="textInput">
      <button @click="display">DISPLAY</button>
    </div>
    <script>
      const application = Vue.createApp({
        data() {
          return {
            newData: 'whatever'
          }
        },
        methods: {
          display() {
            console.log(this.$refs.textInput.value)
          }
        }
      });

      application.mount('#app')
    </script>

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

Attempting to fetch JSON data using the Vue Axios library

I'm attempting to import a local JSON file named blogs.json from the static directory, which contains a collection of blog posts. Currently, I am fetching the blogs using Vue Axios, a module integrated into Nuxt JS. While the blogs are loading corre ...

There was an error in the code: Unexpected token M

Is there a solution for this error that anyone can share? The Google Developer Tools are not able to identify the exact location of the problematic code, making troubleshooting difficult. I am using Meteor and MongoDB. I have looked into Unexpected toke ...

Having trouble with Vue component registration repeatedly failing

Currently, I am working on a front-end project using [THIS VUE TEMPLATE][https://www.creative-tim.com/product/vue-material-dashboard-pro] The issue I am facing involves trying to register a component locally and encountering the following error: "1 ...

Navigate to a specified div using JavaScript

I'm having an issue with my navigation bar not scrolling to the designated div. Despite looking at other examples, I can't seem to find a solution <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"> ...

"Angular's ui-router allows for nested views with individual templates and controllers for each

I have the following list of files: index.html car.html truck.html mainCtrl.js carCtrl.js truckCtrl.js I aim to create the following routes: #/search (template: index.html, controller: mainCtrl.js) #/search/car (template: car.html, controller: carCtrl. ...

Is it possible to determine if the selected/clicked element is a multiple of "n" using jQuery?

If I have 'n' li elements within a ul, I am looking to trigger an alert message only when the li item that is selected or clicked on is a multiple of "n" (for example 3). In this scenario, the alert should only appear if I click on the 3rd, 6th, ...

Strange problem with Mongoose and MongoDB arises while operating MEAN application on Openshift platform

My journey with Openshift has been quite eventful. From making drastic changes to learning about version-ing and nvm (Node Version Manager), it has been a rollercoaster ride. Dealing with npm versions to ensure compatibility with the server used express ve ...

Tips for converting ActiveRecord hierarchy into a JavaScript array

Currently, I am in the process of incorporating a treeview feature into my institution model within my rails application. To accomplish this, I have integrated the ancestry gem into my model successfully and included the treeview component into my view wit ...

Looking to fill every space? Try using React with MUI Grid!

I am currently using Material UI Grid to create an image grid, and I am facing challenges in eliminating the gaps between certain grid items. Despite attempting to modify the alignitems and justify values of the container, I have not been successful in r ...

Guidance on redirecting and displaying the URL retrieved from an API response object in the browser using express and axios

Currently in the process of developing a payment gateway using the Paystack API along with Axios for managing HTTP requests. After thoroughly examining their API documentation and reviewing their Postman collection, I was able to grasp how to structure th ...

Creating a Div element to occupy an entire row is a simple task that can be achieved

Seeking a solution to automatically generate a DIV when our products do not fill up the entire row, using the MixItUp jQuery filter and sort plugin. Is there a JavaScript code that can detect the empty space in a row and create the necessary div to mainta ...

When the if statement fails to halt the code from running

After diving into the world of coding just one or two weeks ago, I decided to create a feature where users can press a key to start playing. No matter what I try, I always end up with the same outcome. Here's my approach: I have an if statement that ...

Executing a function on each page within the head tag in Nuxt.js

I successfully made my function accessible by using the plugin attribute in the nuxt.config.js file, allowing me to call the function under mounted on every page. I am looking for a more efficient way to run this function within the head tag and have it b ...

Disabling the authentication prompt in the browser

Apologies for the repetition, but I would like to pose a more general question. Is there any method on the client side of a web application to predict if requesting a resource will result in a 401 status code and trigger an unattractive authentication pro ...

Having trouble printing a section of a webpage after making CSS adjustments

When trying to print part of a page, I used the following method. It successfully prints that portion of the page, however, it does not preserve the CSS effects. <body> <h1><b><center>This is a test page for printing</center&g ...

Using Typescript to establish a connection between ngModel and an object's property

Let's talk about how we can dynamically bind an input to an undefined property in an object. For example, we have an object named user: let user = {}; How can we bind an input to a property that doesn't exist yet? Like this: <input [(ngMode ...

Ensuring jQuery filter() works seamlessly with Internet Explorer versions 6, 7, and 8

On my webpage, I have implemented an ajax call using the jQuery library to handle a specific task. After making the ajax call, I need to parse the response message that is returned. However, I encountered an issue with Internet Explorer versions 6, 7, and ...

Is it acceptable to post variables using AJAX for processing?

Is there a way to send data with AJAX using the code provided? It seems like the information is not being sent to the PHP file as intended. Here is the code snippet: $('#send').click(function(){ var pseudo = $('#psd').val(); }) ...

I'm looking to leverage axios and useState-useEffect to collect data from numerous web pages. Any suggestions on how to do this efficiently

I stumbled upon a URL that leads to an API with a total of 42 pages and 826 data entries. The URL is . My goal is to store all the data in one variable for future filtering purposes, especially when implementing a "show more" button feature. Initially, on ...

Leveraging the node CLI tool as a library for trimming MP3 files (trimp3

I recently came across a fantastic library that I am interested in using for my nodejs project: https://github.com/kyr0/trimp3 The only issue is that it functions as a cli tool, and I would like to integrate it seamlessly into my codebase as a library. D ...