Can the @keyup event be utilized to target an input element within a customized component?

Hey there! I've been trying to target an input element within a custom component using v-on:keyup, but it seems like I'm having some trouble.

<TextField label="Some Text" @keyup="updateLen" v-model="text"  />

//javascript
computed: {
updatedLen(event) {
  let target = event.value.length;

  console.log('target', target)
},
}

Below is the source code for the custom component:

<template>
<div>
<v-text-field>@keyup="keyUp"</v-text-field>
</div>
</template>

<script>
keyUp(event)
    {
      this.content = this.content.replace(/([^a-z0-9-]+)/gi, '');
        
      this.$emit('input', this.content);
     
    },
</script>

Answer №1

Listeners on custom components, such as @myEvent, listen for custom events emitted using this.$emit. In this case, @keyup is listening for the custom event this.$emit('keyup') (Note: This is not the native DOM keyup event.)

If your TextField component does not emit a keyup event, the listener will never be triggered. Even if a component inside it triggers an event with the same name, that event is only dispatched to its direct parent (i.e. the component using it).

To make the listener trigger, you need to emit the event while listening to it.

<TextField label="Some Text" @keyup="updateLen" v-model="text"  />
<template>
  <div>
    <v-text-field @keyup="keyUp"></v-text-field>
  </div>
</template>

<script>
  keyUp(event) {
     this.content = this.content.replace(/([^a-z0-9-]+)/gi, '');
     this.$emit('input', this.content); // Updates `v-model`
     this.$emit('keyup', event); // Triggers your `@keyup` listener
  },
</script>

If your TextField component is simply a wrapper for v-text-field and you want it to have the same props / listeners, you can disable the automatic inheritance of attributes and set them yourself.

<template>
  <div>
    <v-text-field v-on="$listeners" v-bind="$attrs">
  </div>
</template>

<script>
export default {
   inheritAttrs: false, // The root element won't inherit all static attrs
}
</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

Can the warning about a missing dependency in useEffect be incorrect at times?

After using hooks for some time, I still don't quite grasp why React insists on including certain dependencies in my useEffect that I don't want. My understanding of the 'dependencies' in a useEffect hook is as follows: You should add ...

Check to see if the cursor is positioned on the newly created raphael element without requiring a new mouse

After creating a new element using Raphael.js, I have noticed that hover events do not trigger on Chrome (42.0.2311.90) or Internet Explorer (11.0.8) when the element is under an idle cursor. However, Firefox (31.0, 32.0.2, and 37.0.2) works as intended. ...

The 'property' is not found within the type '{ my_function(): void; }'

I am just starting out with TypeScript and VueJS. Currently, I am pondering the best approach for setting the type of a JSON key that should start off as null. <script lang="ts"> import Vue from 'vue'; export default Vue. ...

Adding an element to the second-to-last position in a list

In the context of a HTML unordered list <ul id = "master_key_list"> <li id="master_key_23" class="available_element">Fix the Peanut Butter</li> <li id="master_key_24" class="available_element">Focus on Price Sensitivity</li& ...

What is the best way to modify the display of a specific section on a page in VueJs 3?

I have three main sections on my webpage, and I am looking to change the view for just one section: <template> <div id="main"> <div id="scene"> <scene/> </div> < ...

Learn how to use Node.JS Express to write to a file from a static file by implementing server-side JavaScript

I am currently working on a node.js project where I can successfully write to a file from the app.js file. The app.js file starts the server and executes the content of index.html located in my public folder. However, I am facing an issue where I am unable ...

Incorporating dynamic images from local sources into a listview in a React Native

I'm currently having an issue with loading local images in a React Native app that I'm developing. As per the instructions provided in the documentation at https://facebook.github.io/react-native/docs/images.html It's mentioned in the guid ...

How to Display Element in Vue.js when Window.print is Triggered?

I'm wondering if it's possible to show a different message when a user tries to print my page. I currently have a 'Pay Now' button that isn't visible in the print preview. I'd like to display a text saying 'Visit www.exam ...

"Enhancing Your Website Navigation with jQuery: Incorporating Mouse Events into Your Menu

My objective is to create a simple mouseover effect on my menu that remains active while the mouse is in a submenu, and triggers the close() function when the mouse leaves the main tab or the submenu. I understand that an event handler is required to trig ...

Why isn't the import statement fetching the necessary function for me?

Hello, I am currently utilizing NextAuth for my application: The main file I am attempting to pull data into: [...nextauth].js import NextAuth from "next-auth" import Providers from "next-auth/providers" export default NextAuth({ // ...

Detecting changes in Excel columns or rows using Office.js

In the realm of Excel Add-ins, I am currently developing a project using Angular and TypeScript. The primary objective is to establish a connection between values within the add-in and the spreadsheet, ensuring that users have access to information on whic ...

Struggling to concentrate using jQuery?

When the search icon is clicked, I want the focus to be on the input so the user can start typing right away without having to manually click on it. Although I tried using focus(), it doesn't seem to work for this particular input. $('.header__ic ...

Can a dynamic react component be inserted into a standalone HTML document without the need for file downloads or server support?

I am implementing React for a Single Page Application (SPA) project where users can customize a component's font size, color, etc., and then embed this customized component into their website. I'm looking for a way to bundle the component and pre ...

Generate a new div element dynamicaly after every 10 elements are added

I am facing the following scenario: I need to dynamically add an UNKNOWN number of checkboxes to a layout component, arranged in columns with 10 checkboxes per layout item. Although I have successfully added all the checkboxes to a single div element, I ...

Swapping out the initial icon in a v-list-item with Vuetify

I have a list of items with icons that need to change when clicked. The issue I am facing is that all icons are changing at once because they share the same v-model. How can I modify my code so that only the icon being clicked changes? Here is my current i ...

The Failure of window.pushState in HTML 5 History

Looking to implement ajax loading for updating center content and URL without refreshing the page. Everything seems to be working fine except for the history management. It appears that window.pushState is not properly recording URLs or the popstate even ...

Is it possible to develop an image that can be zoomed in and out using the mouse

$(document.createElement('img')) .width(imgW) .height(imgH) .addClass('img_full') .attr('src', $(this).attr('data-src')) .draggable() .css({ &a ...

Bootstrap encountered an issue with altering header information

I've been trying to set up a notification system that plays a sound every time someone makes a new post. I'm working with Bootstrap 4 and I've tried inserting JavaScript into the functions page, but no matter how many times I call the ' ...

Instead of relying on Vue TypeScript, we are leveraging IntelliJ with TypeScript 5.0.3 to compile our Vue project

My current version of IntelliJ IDEA is 2023.1 (Ultimate Edition) Build #IU-231.8109.175, released on March 28, 2023. I am facing an issue where my project fails to compile using "Vue TypeScript", resulting in some type mismatches being overlooked. In the ...

Retrieving the computed value created from an axios get request in Vue

I am attempting to retrieve the computed value in created for a GET request. I have declared the classroomBackground as a string in my data. data() { return { classroomBackground: '' as string, ...