Executing a JavaScript function within a Vue component script

I'm working on a simple component file for submitting a form, along with a JavaScript function to handle an action:

<template>
    <div>
       <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal">×</button>
            <h4 class="modal-title">Create Job</h4>
        </div>
        <div class="modal-body">
            <form method="post" @submit.prevent="signIn" @keydown="errors.clear($event.target.name)">
                <div class="form-group">
                    <label>Job Name</label>
                    <input type="text" class="form-control" placeholder="Web Developer" v-model="name">
                    <span v-text="errors.get('name')" class="text-danger help-block"></span>
                </div>

                <!-- Rest of the form fields -->

            </form>
        </div>
        <div class="modal-footer">
            <button type="button" class="btn btn-info btn-fill btn-wd" v-on:click="addJob">Save</button>
            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        </div>
    </div>
</template>

<script>

    <!-- JavaScript logic here -->

</script>

In another file that renders the Vue component file, I have a JavaScript function called getJobTable() for DataTable functionality:

<script>
    $(function() {
        var getJobTable = function(){
            // DataTable logic
        }
        getJobTable();
    });
</script>

To access the getJobTable(); function inside my Vue component file, you can create a global variable in your main HTML file and then call it from within your Vue component using window object:

<script>
    mounted: function()
    {
        window.getJobTable();
    }
</script>

Answer №1

One way to make getJobTable accessible from anywhere is by moving the function definition out of the jquery wrapper and keeping only the call inside.

var getJobTable = function() {
  // ...
}

// ...

$(function() {
  getJobTable();
})

//...

methods: {
  addjob: function() {
    // ...
    getJobTable();
  }
}

You can see an example of this in action in this CodePen

Answer №2

To ensure that your script inside a Vue component can access the getJobTable() function, you can declare it as global.

In your external JavaScript code:

window.getJobTable = function() {// ...}

Within your Vue.js component:

 methods: { 
   addjob: function() {
     // ...
     window.getJobTable();
   }
}

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

Displaying an 'undefined' message in a JavaScript alert box

Hello! Just made the switch from C#/C++ to JavaScript recently and I'm really enjoying it. I've encountered a behavior that has me scratching my head, can anyone help explain? So here's what's happening: when I run this script, I see ...

Show only the image source (img src) and do not display the audio and video sources using jQuery

In my code, I need the following conditions to be met: when an image is posted from the backend, the video and audio sources should automatically hide; when a video is posted, the image and audio sources should hide; and when an audio file is posted, the v ...

Guide on converting AS3 to HTML5 while incorporating external AS filesAlternatively: Steps for transforming AS

I've been given the challenging task of transforming a large and complex Flash project into html5 and javaScript. The main stumbling block I'm facing is its heavy reliance on external .as files, leaving me uncertain about the best approach. Most ...

The absence of "_ssgManifest.js" and "_buildManifest.js" files in a Next.js application deployed on Google Cloud Platform was discovered

Upon opening the website, there are instances where the console displays the following errors: GET https://example.com/subpath/_next/static/9ufj5kFJf/_buildManifest.js [HTTP/3 404 Not Found 311ms] GET https://example.com/subpath/_next/static/9ufj5kFJf/_ss ...

Response from the controller upon choosing a value from the selection dropdown

Scenario: In this scenario, there are two tables in consideration: Firm table : ID (string), Firm(string) Firms table: FirmID(string FK), Name(string) The goal is to select a value from the Firm table, pass it to the controller as Firm, and then execut ...

What is the process for obtaining the cypress interception fixture twice but in two distinct formats?

Hi there, I'm new to Cypress and facing a challenge that I hope you can help me with. In my test file, I have the following code: cy.SetupClassificationsStubFixture(1); This code refers to a custom command that I've created: Cypress.Commands.ad ...

css problem with scaling in firefox

My goal is to create a website that adjusts its size based on the document size. When the document size is between 0px and 1024px, I want the design to be responsive. This can easily be achieved with CSS media queries. Similarly, when the document size is ...

Popup showing values that are not defined

I've encountered an issue with tooltips on my bar graph. The tooltips should display the values of boarding and alightings corresponding to each stopname column, but I'm seeing undefined values like this Below is my code snippet: <!DOCTY ...

Bot in Discord designed to provide varying tiered rewards for designated roles on payday

I'm trying to configure the bot to distribute different amounts of payment to various roles. However, no matter what I attempt, it keeps indicating that the role is undefined. Being new to JavaScript and coding in general, I've researched this ...

How can I integrate vue-cli output into a PHP project?

One benefit of using vue-cli is that it automatically sets up a local server for you. I'm curious, can I utilize the output from vue-cli in a PHP project (such as app.js )? Another question I have is related to the local network - How does it suppo ...

I'm baffled on how to find access to ParametricGeometry within Three.js

I've been looking into how to access ParametricGeometry because I keep encountering this message when I attempt to use it: "THREE.ParametricGeometry has been relocated to /examples/jsm/geometries/ParametricGeometry.js" Any ideas on how to do this wou ...

The function passed to the modal is not functioning correctly when stored within the state

Is it possible to create a unique modal that can be utilized for multiple actions on the same page? To achieve this, I have implemented a state to store the title, description, and submit function for modal click and modal open state. In addition, I want t ...

Manipulating the display style of an element without using jQuery

Could anyone assist me with this issue? I am currently facing a challenge in making the following script functional. It is intended to hide the button after it has been clicked. The script is being called through Ajax and PHP, so I am unable to utilize jQ ...

Tips for avoiding the push method from replacing my items within an array?

Currently, I am diving into Typescript and VueJS, where I encountered an issue with pushing elements to my array. It seems to constantly override the 'name' property. Let me share the code snippet causing this problem: const itemsSelectedOptions ...

What is the best way to iterate through an array of arrays using a foreach loop to calculate the total number of specific properties?

For instance, if YieldCalcValues were to look something like this: [ [ 850, 500 ], [ 3, 6 ], [ 1200, 5000 ], [ 526170, 526170 ] ] I am looking to create a foreach loop that calculates the yield per for each product. How can I accomplish this correctly? l ...

Get connected to your favorite music on iTunes without the hassle of opening a new window by simply clicking on the

Is there a way to call a link to iTunes (itms://...) without opening a new window? I've tried using window.open but that just opens a new window. Also, $.get('itms://...'); doesn't seem to work for me. Are there any other options avail ...

Instead of using a v-if condition, add a condition directly in the Vue attribute

Apologies for the unclear title, as I am unsure of what to name it with regards to my current issue, I am attempting to create a component layout using vuetify grid. I have a clear idea of how to do this conventionally, like so: <template> <v-fl ...

Switching between blog posts in a Vue application by clicking on a link from the sidebar labeled "Recent Updates."

Having an issue with my personal project, I created a BlogPost.vue file where I display the post that was previously clicked on from a Blog.vue page. In this BlogPost component, I showcase the title and content of the selected post, while also featuring a ...

What could be causing React to throw an error?

Check out this React Component: GeneralInformation = React.createClass({ totalCaptialRaisedPanel: function() { var offeringInfo = this.props.company.data.offerings.data[0]; var percentageComplete = (offeringInfo.capital_raised / offer ...

"Exploring the power of Vue3's composables in rendering

Can a composable function be created to utilize a render function for displaying content? For example: import { h } from 'vue' export function useErrorHandling() { return { render() { return h('div', { class: 'bar&a ...