What is the VueJS 3 alternative to the JS Select() function?

I am having trouble selecting the password textbox in my code when it does not match with the confirm password textbox after clicking the register button. Click here to see the image of the desired output

Is there a similar function to select() in VueJS that I can use? The textbox I want to select is in the ref state.

I attempted value.select() but it returned an error in the console saying it is not a function.

Just to clarify, I am still in the process of learning about VueJS and Quasar. I have been researching and reading documentation but have not found a solution yet.

Here is the code below.

<template>
    <div class="window-height window-width row justify-center items-center">
        <q-card class="my-card">
            <q-card-section class="col-10" style="width: 800px">
                <q-input v-model="nameTextBox" label="Name" />
                <q-input ref="email" v-model="emailTextBox" type="email" label="Email" />
                <q-input ref="password" v-model="passTextBox" type="password" label="Password" />
                <q-input v-model="conPassTextBox" type="password" label="Confirm Password" />
                <br />
                <br />
                <q-btn color="secondary" class="q-mr-lg"
                    @click="registerProfile(emailTextBox, nameTextBox, passTextBox, conPassTextBox)">
                    Register
                </q-btn>
                <q-btn :to="'/'">Cancel</q-btn>
            </q-card-section>
        </q-card>
    </div>
</template>

<script setup>

import { ref, getCurrentInstance } from 'vue'
const { $feathers } = getCurrentInstance().appContext.config.globalProperties

const nameTextBox = ref('')
const emailTextBox = ref('')
const passTextBox = ref('')
const conPassTextBox = ref('')

const checkConfirmPassword = (pass, conPass) => {
  // console.log(pass, conPass)
  if (pass === conPass) {
    return true
  } else {
    return false
  }
}

const registerProfile = (emailText, nameText, passText, conPassText) => {
  console.log(emailText)
  $feathers.service('/users').find({
    query: {
      email: emailText
    }
  })
    .then(() => {
      // email found
      alert('Email has already been registered. Please use another email.')
      // ref.email.select()
    })
    .catch(() => {
      console.log(checkConfirmPassword(passText, conPassText))
      if (checkConfirmPassword(passText, conPassText) === false) {
        alert('Password is the same. Please retype it again.')
        passTextBox.value.select() // -> This syntax does not work.
      } else {
        console.log('Passed')
        $feathers.service('/users').create({
          email: emailText,
          name: nameText,
          password: passText
        })
      }
    })
}

</script>

Answer №1

When manipulating the password textbox value, ensure you are using the correct method. Define your ref instances and use them accordingly. First, define your ref names:

const password = ref(null)
const email = ref(null)

Then, in the HTML section, make sure to use the exact names for the ref prop:

<q-input ref="email" v-model="emailTextBox" type="email" label="Email" />
<q-input ref="password" v-model="passTextBox" type="password" label="Password" />

Lastly, utilize these variables to reference elements:

password.value.select()
// or
email.value.select()

The full code snippet would look like this:

<template>
    <div class="window-height window-width row justify-center items-center">
        <q-card class="my-card">
            <q-card-section class="col-10" style="width: 800px">
                <q-input v-model="nameTextBox" label="Name" />
                <q-input ref="email" v-model="emailTextBox" type="email" label="Email" />
                <q-input ref="password" v-model="passTextBox" type="password" label="Password" />
                <q-input v-model="conPassTextBox" type="password" label="Confirm Password" />
                <br />
                <br />
                <q-btn color="secondary" class="q-mr-lg"
                    @click="registerProfile(emailTextBox, nameTextBox, passTextBox, conPassTextBox)">
                    Register
                </q-btn>
                <q-btn :to="'/'">Cancel</q-btn>
            </q-card-section>
        </q-card>
    </div>
</template>

<script setup>

import { ref, getCurrentInstance } from 'vue'
const { $feathers } = getCurrentInstance().appContext.config.globalProperties

const nameTextBox = ref('')
const emailTextBox = ref('')
const passTextBox = ref('')
const conPassTextBox = ref('')

// Define refs to components. 
// Use this variable name as the one you set in the ref prop for the component.
const password = ref(null)
const email = ref(null)

const checkConfirmPassword = (pass, conPass) => {
  // console.log(pass, conPass)
  if (pass === conPass) {
    return true
  } else {
    return false
  }
}

const registerProfile = (emailText, nameText, passText, conPassText) => {
  console.log(emailText)
  $feathers.service('/users').find({
    query: {
      email: emailText
    }
  })
    .then(() => {
      // email found
      alert('Email has already been registered. Please use another email.')
      email.value.select() // this works either
    })
    .catch(() => {
      console.log(checkConfirmPassword(passText, conPassText))
      if (checkConfirmPassword(passText, conPassText) === false) {
        alert('Password is the same. Please retype it again.')

        password.value.select() // -> This works now.

      } else {
        console.log('Passed')
        $feathers.service('/users').create({
          email: emailText,
          name: nameText,
          password: passText
        })
      }
    })
}

For more information on how refs work in Vue3 and the composition API, please refer to this link.

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

Stylized search input inspired by Pinterest with a bubbly design

When it comes to my search bar, I want the user's entered keywords to be displayed within a bubble that has a delete option when they press space to add another keyword. This functionality is similar to what Pinterest does with their search bar, as de ...

How can I retrieve and process a text or .csv file that has been uploaded using Multer?

Just starting out with Multer - I created an application to analyze a .csv file and determine the frequency of each keyword and phrase in the document. However, I have since made adjustments using node and express to analyse the file AFTER it has been subm ...

Position the read more buttons in JavaScript at the bottom of the div

I designed three boxes in this section with content inside. To enhance the functionality, I added a feature that made the boxes smaller. Everything was functioning perfectly, but I encountered an issue with the alignment of the buttons at the bottom. Is th ...

Where does the 'Execution Context Destroyed' error originate from in my code?

Currently, I am developing a program to extract forum responses for the online University where I am employed. While I have managed to successfully navigate to the relevant pages, I encountered an issue when trying to include scraping for the list of learn ...

Using `window.location.href` will terminate any pending asynchronous calls to the API

Before all async calls to the API are completed, window.location.href is triggered when the code runs. Setting a breakpoint on the location resolves the issue. How can I ensure that all calls are finished before invoking window.location.href? Code: const ...

Is it possible to utilize ag-grid's API to filter multiple checkbox values simultaneously?

I am currently utilizing angularjs and have implemented a series of checkboxes to filter my ag-grid. So far, I have successfully utilized radio buttons and the api.setQuickFilter method for filtering based on individual values. However, I am facing an iss ...

Is it possible to execute a function once an element has finished loading?

I am facing a challenge with running the List_Sandbox functions after a specific each loop. This loop selects checkboxes that trigger a change event, rendering additional checkboxes that need to be selected. The issue arises when the List_Sandbox functio ...

Tips for handling ng-if during element presence checks

There's a hidden div on my web page that is controlled by an ng-if directive. I'm looking to create a test that confirms the presence of the element only when it should be visible. If the condition set by ng-if is not met, the element is complete ...

Conceal a child div through the use of AJAX technology

Utilizing AJAX, I am loading pages on my website. Each page consists of two common div elements - the header div and the content div. When navigating from one page to another, I load the content div of the next page into the current page using the followin ...

Out of the total 1341 test cases, Yarn is currently only running 361

Currently, I am immersed in a project containing over 1300 test cases. Despite my efforts to clone the entire project again, it appears there is an issue with my npm. Upon executing yarn test, only 361 out of 1341 tests are running. I am puzzled as to how ...

Switch out the Select feature for Checkboxes

Hey there! I'm currently trying to enhance the style and functionality of the Select checkboxes on my blog. However, when I change them to checkboxes, they lose their original function of searching for selected tags in each Select option. This issue i ...

Is it secure to edit and save user HTML using JavaScript?

Consider this scenario: I've developed a cutting-edge tool for creating forms with Javascript. Users can utilize links to integrate html elements (such as input fields) and leverage TinyMCE for text editing. The data is saved through an autosave featu ...

Calculate the sum of a specific column in an HTML table using JavaScript after

I have a function called CalColumnHistDEPOSITO() that I use to sum a table column as it loads from the server side into my HTML page. However, when I apply a filter, it continues to sum the entire table ignoring the filter. (function() { 'use stric ...

A guide on retrieving an integer from a JavaScript prompt using Selenium with Python

Currently, I'm attempting to develop a prompt that asks the user for a number on a webpage by utilizing Selenium in Python. Below is the code I've implemented, however, it is returning a None value. driver = webdriver.Chrome() driver.get(' ...

Guide to utilizing Vuex store to update values from a child component (settings page) in a Vue.js application

I am currently working on implementing a "settings" component that will store selected values in a centralized location for other components to access and update their appearance accordingly. SettingsView.vue: One of the settings (you can also view it on ...

Execute CGI upon the loading of the page

My goal is to achieve the following: When the HTML page loads, I want to execute a CGI script written in C to call a specific function. My current approach involves using a JavaScript function in the HTML: <body onload="MyJsFunc();"> Then, in th ...

Obtaining the NativeElement of a component in Angular 7 Jasmine unit tests

Within the HTML of my main component, there is a my-grid component present. Main.component.html: <my-grid id="myDataGrid" [config]="gridOptions" </my-grid> In main.component.specs.ts, how can I access the NativeElement of my-grid? Cu ...

Executing a method in an applet using JavaScript

I am trying to print some information using an applet. The applet is located in the signed qds-client.jar file and has the following code: public class PrintText extends Applet implements Printable { private ClientAccount clientAccount; public Client ...

Redirecting from HTTP to HTTPS with node.js/Express

Are there steps I can take to modify my web application to operate on HTTPS instead of HTTP using node.js/express? I require it to run on HTTPS due to the use of geolocation, which Chrome no longer supports unless served from a secure context like HTTPS. ...

Tips for updating the color of buttons after they have been selected, along with a question regarding input

I need help with a code that changes the color of selected buttons on each line. Each line should have only one selected button, and I also want to add an input button using AngularJS ng-click. I am using AngularJS for summarizing the buttons at the end ...