Is there a way to trigger a function after Vue makes changes to the DOM?

There is a select box that displays either one of the two select boxes based on its value change:

<form id="reportForm">
    <select class="form-control chosen-enabled"
            v-model="selectedDataType">
        <option value="First">Show First</option>
        <option value="Second">Show Second</option>
    </select>

    <select class="form-control chosen-enabled"
            v-if="selectedDataType === 'First'">
        <option value="one">One</option>
        <option value="two">Two</option>
        <option value="three">Three</option>
    </select>

    <select class="form-control chosen-enabled"
            v-if="selectedDataType === 'Second'">
        <option value="foo">Foo</option>
        <option value="baz">Baz</option>
        <option value="bar">Bar</option>
    </select>
</form>

The implementation in Vue js is fairly straightforward:

var app = new Vue({
    el: '#reportForm',
    data() {
        return {
            selectedDataType: '',
        }
    }
});

Everything is functioning correctly.

However, along with this, the Chosen JQuery library is also being used, which requires calling $('.some-selector').chosen().

Since these DOM elements are dynamically added by Vue, they are new and must have the $(...).chosen() method invoked on them to enable the Chosen functionality.

How can I manipulate DOM elements without disrupting Chosen?

Answer №1

Implementing a watcher function allows you to monitor a specific variable for changes and then trigger the corresponding method. You can learn more about this concept here

Answer №2

To effectively utilize the Chosen jQuery plugin, it is crucial that the elements are present in the document at all times. For optimal control over their display, it is recommended to use v-show instead of v-if.

By employing v-show, you ensure that your elements remain within the document structure, allowing the jQuery plugin to seamlessly interact with them.

<select class="form-control chosen-enabled" v-show="selectedDataType === 'First'">
  <option value="one">One</option>
  <option value="two">Two</option>
  <option value="three">Three</option>
</select>

<select class="form-control chosen-enabled" v-show="selectedDataType === 'Second'">
  <option value="foo">Foo</option>
  <option value="baz">Baz</option>
  <option value="bar">Bar</option>
</select>

For more information, visit https://v2.vuejs.org/v2/guide/conditional.html#v-if-vs-v-show

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

Is there a way to verify that a form field has been completed?

Currently, I am grappling with a method to clear a field if a specific field is filled in and vice versa. This form identifies urgent care locations based on the information provided by users. The required entries include the name of the urgent care facil ...

JavaScript-enabled tests encountering issues (Bootstrap 3, Rails 4, Travis CI)

I'm encountering a strange error that only shows up in my CI environment. This error doesn't occur in development, production, or local test environments. ActionController::RoutingError: No route matches [GET] "/fonts/bootstrap/glyphicons-halfli ...

Vue JS encounters an HTTP 500 Internal Server Error when communicating with the

I have created an Asp.Net Core Web API and confirmed its functionality using Postman. After downloading the GitHub Project from this blog: , I updated the url in src/backend/vue-axios/axios.js to match my web api url. However, all I am getting is: HTTP500: ...

The input value "HH:MM" does not match the expected format of 'FormatOptions' for this parameter

I created a function that takes in two parameters: data and format. I am attempting to define an ENUM(FormatOptions) for the "format" parameter. However, I encountered the following error: Argument of type '"HH:MM"' is not compatible with param ...

What is the process for deducting the ordered quantity from the available quantity once an order is confirmed

Although I'm not a fan of hard coding, I've been struggling to find a solution to my problem and some explanations are just not clicking for me. The issue at hand involves three data products in the cart, product details, and placed order data f ...

Is it possible to acquire Axios Response prior to Vue Component rendering?

I need to utilize: homePage.image = 'storage/' + 'rkiGXBj7KJSOtsR5jiYTvNOajnzo7MlRAoXOXe3V.jpg' within: <div class="home-image" :style="{'background-image': 'url(' + homePage.image + ')'} ...

Send the data from the table to the controller

As a beginner in programming, I am trying to send a table input value to the controller. I have attempted the following method: $("#btnsend").click(function () { $.ajax({ type: "POST", contentType: "application/json ; charset=utf-8", ...

What are the steps to turn off the rule .MuiInputBase-inputType-121 in Material UI TextField for email input field because of a display problem?

Here is a snippet of JSX code: import TextField from '@material-ui/core/TextField/TextField'; <Col xs={6}> <TextField pattern=".{3,}" name="fullName" ...

What steps should I take to incorporate the delete feature as described above?

Click here to access the delete functionality Hello there, I need help implementing a delete function similar to the one provided in the link above. Below is the code snippet for your reference. I am utilizing the map method to extract data from an array ...

Experiencing difficulty with setting up the ReactJS pagination component

Each time I click on a page number, the maximum size of the pages is returned as a handler parameter (for example, clicking on 2 returns 4). What could be causing this issue? UPDATE class PagingControl extends React.Component{ constructor(props) { ...

How can we create a stylish HTML table using CSS display:table and SASS in a sophisticated manner?

Is there a more stylish approach to styling this HTML table using CSS display: table with Sass? Instead of using traditional table elements, I have created a table layout using display: table CSS styles applied to div elements: <div style="display: ...

Securing Codeigniter with CSRF protection while using datatables and an AJAX URL

I am currently utilizing codeigniter and have implemented CSRF protection. We have enabled it using the following configuration: $config['csrf_protection'] = TRUE; For form submission, we are using the following code: `<input type="hidden" ...

Make sure the inputs in separate table data cells are lined up in

I need help aligning two input fields in separate td elements to be on the same line. The issue I am encountering is that when an input is added to a td, it covers up any text within the td. https://i.stack.imgur.com/c7GiQ.png There are two scenarios: I ...

Could it be a cross-domain problem with jQuery?

Could this issue be related to cross-domain problems? I am attempting to utilize $.ajax to load a file. However, I noticed that for some files, the readyState is showing up as 4, while for others it is displaying as 1. Currently, my jasmine tests are runn ...

What is the reason for NextJS/React showing the message "You probably didn't export your component from the file where it was declared"?

What's the Issue The error code I am encountering Warning: React.jsx: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the ...

What are some effective strategies for bypassing CORS requirements in Vue client-side and server-side applications?

I found this amazing MEVN stack tutorial that I've been following: MEVN Stack Tutorial The tutorial is about creating a blog post app, where the client side is built using Vue (referred to as app A) and the backend is built on Express.js providing d ...

JS: Use either $addToSet or $pull based on the current availability of the value

I am looking for a more efficient way to add or remove an ID from an array (target) in an Articles object, based on whether it already exists. Currently, I am using the following approach: var isExisting = Articles.findOne({ _id }).target.indexOf(mID) > ...

Converting Python code into JavaScript for use on an iPhone device

Having a web application with backend developed in Python (using Django) and front end in HTML5 & JavaScript, where Python generated data is transferred to JavaScript/HTML through JSON. Now, I want to transform it into a mobile application, starting with ...

Adjust the font size based on the dimensions of the container

I'm currently working on a script that dynamically sets the font-size based on the container's dimensions and the length of the text. This is what I have implemented so far. window.onload = function () { var defaultDimensions = 300; ...

Having trouble passing arguments to button methods in jasmine when applying vue and moment libraries

I am working on unit testing a Vue app using `jasmine` and `karma`. Here is an example of the code inside one of my components: After fetching data from a database with `v-for=(data,index)`, I am displaying the `data.date` in the template: <p class=&qu ...