Retrieve "event.target" on change using Vue.js and Element UI

Is it possible to retrieve the html field that triggers an event within its corresponding event handler in JavaScript using event.target?

In my scenario, I have a form with:

  • an input element linked to a function on the change event
  • a function responsible for handling the change event

The code snippet looks like this:

var Main = {
    methods: {
      change(par) {
        console.log("The value is: " + par);
        //HERE I can't get "event.target"
      }
    }
};
var Ctor = Vue.extend(Main)
new Ctor().$mount('#app')
<script src="//unpkg.com/vue/dist/vue.js"></script>
<script src="//unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b5d0d9d0d8d0dbc198c0dcf5849b819b8598d7d0c1d49b84">[email protected]</a>/lib/index.js"></script>
<div id="app">
  <template>
    <el-input @change="change" placeholder="Input something here"/>
  </template>
</div>

While I am aware that I could call different functions and determine the relative input, I prefer to use a common function.

Upon inspecting the stacktrace, I noticed the following code in element-ui.common.js where the event is triggered:

handleChange: function handleChange(event) {
  this.$emit('change', event.target.value);
},

In this snippet, the element only passes the value to the event handler. Is there any way to access the event.target? Any suggestions are greatly appreciated!

Thank you

Answer №1

To access the value, you can utilize @input.native

var New = {
    methods: {
      modify(event) {
        console.log("The current value is: " + event.target.value);
        //Can't access "event.target" here
      }
    }
};
var Constructor = Vue.extend(New)
new Constructor().$mount('#app')
<script src="//unpkg.com/vue/dist/vue.js"></script>
<script src="//unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="11734e474f475c667f7b5d6c7b66764076754067607b7e75767f76">[email protected]</a>/lib/index.js"></script>
<div id="app">
  <template>
    <el-input @input.native="modify" placeholder="Enter text here"/>
  </template>
</div>

Answer №2

Seems like Element UI is simply consuming the event and providing back only the value. A workaround for this would be to use @keyup.native(change) instead of @change(change), allowing your par parameter to capture the entire event rather than just the value.

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

Tally the number of sub-labels associated with each main label

In my Angular 9 application, I am looking to separate an array based on the lable field. Within each separated array, I would like to determine the count based on the subLable field. This is the array I am working with: [ {"id":1,"socia ...

Utilizing Page Methods

I have encountered an issue while using an ajax callback for a void method. When I try to call another method within the calling method, I get an error. Any assistance with resolving this problem would be greatly appreciated. Here is the code snippet: ...

Steps for updating text within an object in Angular

details = [ { event: "02/01/2019 - [Juan] - D - [Leo]", point: 72 }, { event: "02/01/2019 - [Carlo] - N - [Trish]", point: 92 } ]; I am attempting to modify the text within the titles that contain - N - or - D - The desired outcom ...

"Utilize Chart Bars in CodeIgniter 3 for Dynamic Data Representation

In my quest to showcase the latest additions to my database, I am looking to create a visually appealing chart using bar graphs. This will help me list out the new records along with the respective months of addition. The hurdle that stands in my way is t ...

What role does @next/react-dev-overlay serve in development processes?

Currently, I am diving into a NextJs project. Within the next.config.js file, there is this code snippet: const withTM = require('next-transpile-modules')([ 'some package', 'some package', 'emittery', ...

Verify whether the input includes a specific value or a different one

Can someone help me with a simple task of checking if a textarea contains the value "12" OR "34"? I have tried the code below but it doesn't seem to work. Any suggestions? function check() { if (V1.value == ('12' || '34')) { ...

issue with leaflet vue3 component showing incorrect marker popup when clicked

I am dynamically loading markers based on the bounding box from an API using a Pinia store. These markers are stored in the itemsList property of my map wrapper component map-view. The markers are rendered as circlemarkers inside a Vue-for loop. As I navi ...

Halt execution of routes using backbone.js

Is it feasible to halt route execution within backbone.js solely using the router? I understand there is a callback function for each route where I could verify if routing is permitted, but I am unsure how to prevent execution based on a property (such as ...

Using Typescript to iterate through nested interface data with the `forEach()` method

CountTargetsData serves as an interface for parsing JSON data with multiple levels of nesting, as illustrated below. interface CountTargetsData { data: { [state: string]: { [date: string]: { [index: string]: { [id: string]: { [targets: ...

The click function is not functioning properly when trying to integrate a jQuery Ajax HTTP request

Here is my query: I am trying to make an Ajax request from within a click function using jQuery (as shown in the example below). However, no data is being retrieved even though I am alerting it through the success function. Could you please guide me on wh ...

How can we use the same name for the ng-model as the variable itself instead of just pointing to the variable?

Below is a directive I am working with: .directive('gdInputField', function() { return { //only applies to elements, not attributes. restrict: 'E', template: "<label for='{{name}}'>{{label}}& ...

Exploring the Art of Setting Multiple Cookies in Node.js

I successfully set a cookie using this code snippet. Now, I wonder how to set multiple cookies at once? var headers = {}; headers['Set-Cookie'] = name1+'='+value1; headers['Set-Cookie1'] = name2+'='+value2; res.writ ...

Leveraging react-router for automatic redirection after form submission

My goal is to implement a search functionality on the page where users can enter a search term (name, email, username) and have the page filter out one card from all the cards based on the search value. I believe that upon pressing enter, we should redirec ...

Enhancing Websites with Vimeo Pictures and JavaScript

Having some issues with my thumbnail system. It's supposed to fetch an image from Vimeo and display it in a div. I attempted to create a loop for a nicer look, but unfortunately, it's not working as expected. Can anyone provide assistance? ...

Utilize node.js to run a local .php file within a polling loop

Here is the purpose of my application in brief. I am using a PHP file to read data via an ODBC connection and then write that data to a local database. This PHP file needs to be executed LOCALLY ON THE SERVER every time a loop is processed in my node.js, ...

I am trying to retrieve information from a data array in my code using the .map method, but I encountered an error even though the

import React, { Component } from 'react'; import ProductItem from '../components/ProductItem/ProductItem'; class ProductList extends Component { constructor(props) { super(props) this.renderProductItems =this.rende ...

If the checkbox is selected, include an anonymous email in the field and conceal the field

I am looking to enhance my Feedback form by providing users with the option to submit feedback anonymously. However, my CMS requires both Email and Name fields to be mandatory. To address this, I am considering adding a checkbox that, when selected, auto ...

Error message: Node: TypeError - Attempted to access the 'sort' property of an undefined variable while using findOneAndUpdate() within a find() operation

My code snippet is shown below: var MongoClient = require('mongodb').MongoClient; MongoClient.connect('mongodb://localhost:27017/school', function(err, db) { if (err) throw err; db.collection('students').find().toArray( ...

Find the MD5 hash of the image uploaded using Java WebDriver

I am trying to calculate the MD5 hash of images that are loaded in Firefox webdriver using Java. import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.JavascriptExecutor; String script = "var ...

The Angular JS routes are not properly loading the required file from the provided TemplateURL when receiving a response from Node

I am trying to retrieve data from a MySQL database using node (routes.js) and have the results injected into club.html, which is then dynamically updated in index.html using ng-view. However, it seems that the JSON response from node is displaying directly ...