Show different components depending on immediate modifications and complete or incomplete data entry in Vue.js

I am trying to create a simple form with a search bar and two buttons. The goal is for the fill button to be visible only when there is text in the input, and for the empty button to show when the input is empty. I want the buttons to dynamically display or hide based on the content of the input field, updating instantly with any changes. How can this functionality be achieved using Vue.js?

The desired behavior is for the fill button to appear when the input has text. When clicked, it should clear the input, hiding itself and revealing the empty button. The fill button's purpose is to reset the input when it contains text.

<form class="myform">

  <button type="reset" class="fill">Show when fill </button>
  
  <button class="empty">Show when empty</button>

  <input type="search" class="myinput">

</form>

Answer №1

If you're looking for some code assistance, the snippet below may be of help.

   <template>
      <div id="app">
       <form class="myform">
    
          <button @click.prevent="reset" v-if="search" type="reset" class="fill">Display when content is filled
          </button>
          
          <button v-else  class="empty">Show when field is empty</button>
    
          <input v-model="search" type="search" class="myinput">
    
        </form>
      </div>
    </template>
   <script>
    export default {
      name: "App",
      data() {
        return {
          search: null,
        }
      },
      methods: {
        reset() {
          this.search = null;
        }
      }

    };
    </script>

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

Passing a state object in a POST request body, only to find it arriving empty at the Express server

I'm having an issue with my React form component when making a POST request. The body of the request is showing up as {} even though I'm trying to send my State object. Within my form Component, I am passing the state information to another Reac ...

Make sure that the click event listener is set up on the anchor element so that it also affects its children

Currently, I have implemented a click event listener on my anchor elements. However, the anchors contain a span element within them, and the event listener does not function properly if you click on the span inside the anchor. document.addEventListene ...

Unable to call a basic object's prototype method

Just starting out with node and feeling like I might be overlooking something simple. In my model file, I have a class that creates new object instances in the following way: const mongodb = require('mongodb'); const getDb = require('../util ...

What sets v-model apart from .sync when implemented on a custom component?

I'm struggling to grasp the distinction between v-model and .sync when used on a component. <my-component v-model="myVar"> V-model simplifies the process by binding a variable (myVar) to the 'value' property of the component and list ...

Using d3.js to toggle visibility of bars when clicking on the legend

// Handling the browser onresize event window.onresize = function () { scope.$apply(); }; scope.render = function (data) { var ageNames = d3.keys(data[0]).filter(function (key) { ...

When implementing Firebase Cloud Messaging with React, the token generated by firebase.messaging().getToken() will vary with every refresh

I'm working on a React web app using Gatsby and I want to integrate push notifications through FCM. My firebase-messaging-sw.js service worker is set up, and I'm trying to retrieve a token using the following method in my app: messaging .req ...

Tips for disabling scrolling on a <div> using another <div> as a lock

I am facing an issue with a page where a div is appended to the body of an HTML. The problem is that there are two scrolls appearing - one on the new overlaying div and another behind it. Here is an approximate structure of the page, how can I ensure that ...

What is the best way to place a 3D model at random points on the surface of a sphere while ensuring that it always faces the right direction?

I'm faced with the challenge of placing huts randomly on a spherical world. While this task is feasible, the issue arises when the huts do not sit correctly - their bottom should be in contact with the tile below. I've experimented with the &apos ...

Issue with handling click events on action column in Datatable using jQuery

Utilizing jquery datatable with an action column containing "edit" and "delete" links. The table populates correctly, but encountering an issue when trying to open a bootstrap modal form upon clicking the edit button within the table. However, the modal do ...

When passing the value of "undefined" into a function, keep in mind that the function must be declared in a separate JavaScript file. This

I'm facing an issue with my JavaScript code. I have a file named test.js that contains a function like this: export const a = (data) => { console.log(data) } In addition, I have a functional component called File.js as shown below: import Reac ...

KnockoutJS is not recognizing containerless if binding functionality

I was recently faced with the task of displaying a specific property only if it is defined. If the property is not defined, I needed to show a DIV element containing some instructions. Despite my efforts using $root and the bind property, I couldn't ...

Question inquired regarding a specific line of code in Javascript/Angular

While working in a factory, I am tasked with constructing an HTML page that includes a form. To successfully manipulate the form, I need to access the FormController. After conducting some research online, I managed to achieve my goal using the following l ...

Is it possible to display multiple qTips for the same target using jQuery qTip2?

I am currently utilizing the jquery qtip2 plugin to generate a mouseover tooltip. Here is the code snippet I am using: $(document).ready(function() { $("#optionalProdsImgPreview_1").qtip({ content: "<img src='http://mysite.com/myimg.jpg&ap ...

The JavaScript function is designed to only accept whole numbers as input

Whenever I invoke a JavaScript function and pass along 4 parameters, it only functions properly if the fourth parameter consists of integers exclusively. Below is the code snippet for my onchange function: onchange="weekchange(this, <?php echo $i ?> ...

Vue Progressive Web App pre-caching routes for improved performance

Seeking guidance to ensure I'm on the right track. I've developed a simple web application using Vue CLI with PWA support. Everything appears to be functioning properly, including the install prompt. My objective is to cache certain pages (route ...

What is the reason for function expressions being hoisted when called within Express JS middleware?

It's common knowledge that function declarations are hoisted, allowing them to be called from anywhere in your script. However, this is not the case for function expressions. For instance: test(); const test = () => { console.log(1+3); } ...

Automatically appending textarea value in HTML tag upon form submission via JQuery

When a form is submitted through JQuery, the HTML tag automatically adds textarea value. This is my JQuery code: $('#calling').click(function() { $('#myform').submit(); }); Within my form, there is a textarea element: <textar ...

Ways to extract the ASP.net variable value and place it inside a JavaScript textbox

Currently, I'm in the process of developing Javascript code for an ASP.net page. Within my coding framework, the string "foo" is linked to a string variable called myString. In order to transfer the value of myString to a JavaScript variable, I incl ...

Determining Marker Location Post Drag in Laravel-VUE Component

I have integrated vue-google-maps into my project and it is working well so far. My goal is to have a marker appear when a user searches and selects their area, and then they can drag the marker to the desired position. I have successfully made the marker ...

Verifying the username's availability through AJAX requests

My registration form utilizing AJAX isn't connecting to the database. It seems like I'm missing something important. Let's focus on validating this particular field: Username:<input type="text" name="user" id="user" maxlength="30"> & ...