Implementing keyup event for Enter key in a custom input field in Vue JS

I need to implement the use of keypu.enter in a custom input component within Vue. I want the <input /> element in this component to trigger a function in the parent component when the enter key is pressed during typing.
This is the code for the custom input component:

<input v-on:keyup.enter="$emit('keyup')"/>

Next, here is the code for the main page:

<template>
  <se-input @keyup="function()"/>
</template>

<script>
import inputField from '../components/inputfield.vue'

export default {
  name: 'inputField',
  components: {
      'custom-input': inputField
    },
  },
  methods: {
    function () {
      // Function
    }
  }
}
</script>

Answer №1

Make sure to pass a value to your custom event, and then have the parent component listen for that event :

Vue.component('customInput', {
  template: `
    <div class="">
      <input v-on:keyup.enter="$emit('keyup', $event.target.value)"/>
    </div>
  `
})

new Vue({
  el: '#demo',
  data() {
    return {
      inputValue: null
    }
  },
  methods: {
    handleInput(val) {
      this.inputValue = val
    }
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="demo">
  <h3>{{ inputValue }}</h3>
  <custom-input @keyup="handleInput"/>
</div>

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

Eliminate JSON data prior to displaying information to the user/strip JavaScript object attributes

I'm facing a dilemma and can't seem to figure out why I'm unable to resolve my issue. My SPA is created using AngularJS, Node.JS, and MongoDB (Mongoose). On the client side, I have a registration form for new users. The form includes a text ...

Using Variables in JavaScriptExecutor with Selenium and C#

Currently in my Selenium tests, I am utilizing JavaScriptExecutor as shown below: IJavaScriptExecutor js = driver as IJavaScriptExecutor; string Pixels = "600"; js.ExecuteScript("arguments[0].style='transform: translateX(600px);'&q ...

How to Use WebDriverJS to Target Multiple Elements by the Same ID

For my latest project, I am tasked with creating a test using Selenium, WebDriverJS, and Jasmine. The test is for a function that shows the content of a specific div only when its associated button is clicked. The challenge is to hide all other elements wi ...

Angular's asynchronous HTTP request allows for non-blocking operations when

I'm currently working with Angular 6, and I've encountered an issue while updating my resource instance in the following way: this.user = api.getUser(); public getUser(): User { const myHeader = new HttpHeaders({ 'Authorization' ...

Tips for deactivating a specific control item within an array in Angular 9

I am working on a form that contains a formArray. InitialFrom(): void { this.addElectricMoneyFG = this.fromBuilder.group({ hasAccountNumber: [this.showMoreInfo], moreAccountInfo: ['', null], description: [''], locales: this.fr ...

Error message "The provided ID is invalid" in Video JS and Vue JS

Recently, I've been working on a project that involves vue.js and Laravel. One requirement for this project is to be able to play HLS videos in all browsers, so after some research, I decided to go with video-js. I successfully installed the video pl ...

Implementing a dynamic value for an import in JS/React

One way to import fonts in Next.js is by using the following method: import { Poppins } from "next/font/google"; const setFont = Poppins({weight: ['300'] , subsets: ["latin"] }); You can then integrate the font into your cod ...

Node is throwing a 302 error on Localhost:3000

Looking for some guidance as a beginner trying to create and run a nodejs application. Encountering an error while running server.js via nodemon, the console displays the following: Express server listening on port 3000 Mongoose default connection open t ...

Webpack is displaying an error message stating that a suitable loader may be necessary to handle this specific file type

Encountering the error "A suitable loader to handle this file type may be required" while utilizing Webpack for compiling ES6 and JSX files. Error message in Terminal: A suitable loader to handle this file type may be required. | import React from ' ...

What is the method to mute Vue 3?

Is there a way to turn off all Vue 3 logs and warnings? I'm looking for the Vue 3 equivalent of: Vue.config.silent = true Here is the documentation for the Vue2 silent config ...

Error in Console while Json is not being displayed on the Screen in React Native

Encountering an issue while attempting to display JSON data from mongodb on the screen of a React Native Application. The screen is visible but no data is showing up. Below is the error message along with the code snippet. This is a basic hello world appli ...

Step-by-step guide on entering text into a hidden field with Selenium WebDriver and Java

I am currently utilizing WebDriver in conjunction with Java for automated testing. I have come across a hidden input field within the following HTML code: <input type="hidden" value="" name="body" id=":6b"> My challenge lies in trying to input data ...

Grouping object properties in a new array using Java Script

I'm currently learning Java script and attempting to merge an Array of objects based on certain properties of those objects. For instance, I have the following array which consists of objects with properties a, b, c, pet, and age. I aim to create a n ...

Discovering the orientation of an object in relation to the camera

I'm encountering a challenge in determining the angle of an object in relation to the camera. My goal is to write code for a spaceship with a camera that follows it. While I have successfully made the camera track the ship, there are instances where t ...

My Scope Filter isn't functioning properly; rather than filtering the data, I am just receiving the results of a plain select * from query

Currently, I am working on a small project that involves utilizing Laravel and Vuejs. Specifically, my goal is to create a basic age filter for users. After thorough testing, it appears that the issue lies within my scopeWithFilters function. When I used ...

Why is the JavaScript function working even without any arguments?

During my search for some JS Code, I stumbled upon this piece of code. Check out the code snippet below: arg = "TEST ALERT MESSAGE"; MyFunction(arg); function MyFunction() { alert(arg) } In the above code snippet, the MyFunction() is provided with an a ...

Ensure form input security by implementing jQuery validation with reCAPTCHA

I have a comment submission form that adds data to a database upon submission. Below is the code snippet for the functionality: function reloadRecaptcha() { var publicKey = "*************************************"; var div = "recap"; Recaptcha. ...

Having trouble accessing variable values within the nth-child selector in JavaScript

I am attempting to utilize the value of a variable within the element selector p:nth-child(0). Instead of hardcoding the number as 0, I want to dynamically assign the value of a variable. In this case, the variable is represented by i in a for loop. Howev ...

`Add new text next to the existing text in a label element`

I need help adding new text directly after the Email: and Password: labels using JavaScript. How should I proceed? The text should be placed immediately after Email: and Password: within the two labels. <div class="container"> <div class ...

Is there an easy method to transmit JSON information by utilizing $window.open() with angularjs?

equivalent to unraveling a knot: var data = { name: 'name', info:{ info1: 'uvbiuonns', info2: 'aisbsiece', } } this approach eliminates the need to retrieve data from the server for the popup ...