0

I have a component that contains a form. On submit button click an alert method should appear. However when the button is clicked I only get a Uncaught ReferenceError: greet is not defined in console which disappears very quickly.

<template>
    <div class="text-breakdown">
        <h3 class = "title">Text Breakdown</h3>

        <form onsubmit="greet()">
            <textarea type="text" id = "breakdown-text"
                  placeholder="Enter your text here">
            </textarea>

            <button class = "custom-button dark-button"
                    type="submit">Breakdown</button>
        </form>
    </div>


</template>

<script>
    import axios from 'axios';

    export default  {
        methods:  {
            greet()  {
                alert("Hello!");
            }
        }
    }

</script>

2 Answers 2

1

This is happening because the browser is trying to find the greet method in global scope as you are calling a native onsubmit submit method. You just need to bind the submit event to vue instance using v-on directive like:

<form v-on:submit.prevent="greet">

Or use the v-on shorthand like:

<form @submit.prevent="greet">
Sign up to request clarification or add additional context in comments.

Comments

1

Review the event bindings:

https://v2.vuejs.org/v2/guide/events.html#Event-Modifiers

You will probably want to bind your greet function to the form using Vue syntax and not just generic onsubmit.

At a minimum it could be:

<form :submit="greet"> .... </form>

3 Comments

probably want :submit.prevent="greet" or the page will reload
Thank you. The code in the answer does work - but the page does reload. :submit.prevent however results in: error 'v-bind' directives don't support the modifier 'prevent' vue/valid-v-bind
@submit.prevent="greet"

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.