24

Here is my fiddle: DEMO

There is a button called "fb-send", which has to get clicked on key press on "enter" in the input field. How to achieve this?

<input @keyup.enter="" placeholder="Reply here" class="fb-comment-input" />
<i class="material-icons fb-button-right fb-send">&#x21E8;</i>

Any help would be much appreciated. Thank you.

7 Answers 7

32

You can also call method for fb_send directly on input field:

@keyup.enter="enterClicked()"
Sign up to request clarification or add additional context in comments.

1 Comment

I used the enterClicked() only to detect if the icon was being clicked. So I cannot assign the method directly on key press of Enter :)
10
<Input v-model="value1" size="large" placeholder="large size" @keyup.enter.native="search"></Input>

1 Comment

While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.
10

14 June, 2020

Enter and KeyPress will work both.

Grab the Input fields in a form like this:

<form v-on:keyup.enter="login">
    <input type="email" v-model="email">
    <input type="password" v-model="password">
    <button type="button" @click="login">Login</button>
</form>

Javascript Part:

<script>
export default {
    data() {
        return {
            email: null,
            password: null
        }
    },
    methods: {
        login() {
           console.log("Login is working........");
       }
    }
}
</script>

1 Comment

Mr. Perfectionist: well explained
8

Here is my answer fiddle: ANSWER-DEMO

I solved this using $refs.

new Vue({
    el:'#app',
  methods:{
    enterClicked(){
        alert("Enter clicked")
    },
    trigger () {
        this.$refs.sendReply.click()
    }
  }
})

1 Comment

Thanks! Question: How do you avoid shift+enter or ctr+enter triggering the method 'trigger()'. I want shift+enter to break line.
8

You need to call enterClick method inside input field like below:

<div id="app">
    <input @keyup.enter="enterClicked" placeholder="Reply here" class="fb-comment-input" />
    <i class="fb-send" @click="enterClicked">&#x21E8;</i>
</div>

<script>
    new Vue({
        el:'#app',
        methods:{
          enterClicked(){
             alert("Enter clicked")
          }
        }
    });
</script>

2 Comments

I used the enterClicked() only to detect if the icon was being clicked. So I cannot assign the method directly on key press of Enter :)
The example provided by @IncharaRaveendra is working fine... You can check it here: jsfiddle.net/nosferatu79/52a7ppt2/3
3

Please use @keyup.enter.native=function(), this will work .

native function is a function provided by your browser/server/JavaScript

1 Comment

nowadays: '.native' modifier on 'v-on' directive is deprecated vue/no-deprecated-v-on-native-modifier
-7
        $("form").keypress(function (e) {
            debugger;
            if(e.charCode == 13){
                e.stopImmediatePropagation();
                e.preventDefault();
            }
        });

2 Comments

This answer could be improved with some explanation.
The question was for vuejs which follows MVVM pattern. jQuery is impractical for its scope

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.