1

I have the following vue component where I am changing the class of the parent row based on whether or not an input is focused

<template>
    <div class="form form--login">
        <div class="form__row" :class="{entered: emailEntered}">
            <label class="form__label" for="login-form-email">Email address</label>
            <input type="text" class="form__control form__control--textbox" name="email-address" id="login-form-email" @focus="emailEntered = true" @blur="handleBlur($event, emailEntered)">
        </div>
        <div class="form__row" :class="{entered: passwordEntered}">
            <label class="form__label" for="login-form-password">Password</label>
            <input type="password" class="form__control form__control--textbox form__control--password" name="password" id="login-form-password" @focus="passwordEntered = true" @blur="handleBlur($event, passwordEntered)">
        </div>
    </div>
</template>

<script>
    export default {
        name: 'login-form',
        data() {
            return {
                emailEntered: false,
                passwordEntered: false,
            }
        },
        methods: {
            handleBlur(e, enteredBool) {
                if (e.currentTarget.value.trim() === '') {
                    // this doesn't do anything - I can do an if else statement to change this.passwordEntered or this.emailEntered based on the name of the current target, but how do I change the value by passing it into the method?
                    enteredBool = false;  
                }
            },
        }
    }
</script>

but it doesn't seem to change the variable that is passed into the method - how do I pass a data variable into the method and change it's value? Or should I be doing it in a different way? I don't really want to be doing an if else statement as I may have a form that has got a lot more inputs on and I think that would be really inefficient to maintain

I also thought that I could do something in the @bur as you can do @blur="passwordEntered = false", but I wasn't sure how to check if the field was empty or not

2
  • You have access to your component data in all your methods with this.dataName. Just don't pass them as an argument and use them directly in your methods. Commented Nov 8, 2021 at 12:27
  • 1
    Why can't I pass them in? as I say, instead of the one line of code as shown in the answer, for a form with 15 inputs, it would seem rather inefficient and hard to maintain to have to write an if else statement for each input just to hardcode the this. Commented Nov 8, 2021 at 12:32

1 Answer 1

3

In order to change the variable, you need to refer it using this

 handleBlur(e, enteredBool) {
   if (e.currentTarget.value.trim() === '') {
     this[enteredBool] = false;   //Change added
   }
 },

and the way you pass it should be like

@blur="handleBlur($event, 'emailEntered')" //Added single quotes

and

@blur="handleBlur($event, 'passwordEntered')" //Added single quotes
Sign up to request clarification or add additional context in comments.

1 Comment

Ah brilliant, nice and simple. Thanks. Will accept when it lets me

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.