Reference argument passing

Below is a snippet of JavaScript code I am using:

function insertIntoTable(title, text, sketchFileName, categoryId)
{
    var db = window.openDatabase('xxx', '1.0', 'xxx database', 5*1024*1024);
    db.transaction(
        function (tx) {
            if (sketchFileName == '')
            {
                tx.executeSql('INSERT INTO TableXXX (title, content, created, categoryID) VALUES (?, ?, ?, ?)', 
                            [title, text, Date.now(), categoryId], 
                            function (transaction, resultSet) {
                                if (resultSet.rowsAffected) {
                                    myNameSpace.returnedId = resultSet.insertId;
                                }
                            }, handleSQLError);
            }
        }, handleSQLError);
    );
}

Currently, I am using a global variable, myNameSpace.returnedId, to capture the insertId.

Is there a way to modify the function insertIntoTable to include a new parameter that points to myNameSpace.returnedId?

Answer №1

To access the variable with two arguments, you can use the following function:

function addToTable(title, content, fileName, id, space, variable)
{
    ...
    space[variable] = finalResult.insertId;
}

Invoke it in this manner:

addToTable( ..., mySpace, 'resultId' );

Another approach is to employ a setter function:

function addToTable(title, content, fileName, id, setReturnId)
{
    setReturnId( finalResult.insertId );

}

Invoke it like this:

addToTable( ..., function(value){ mySpace.resultId = value } );

Answer №2

Modify the function as shown below:

function addToTable(title, content, file, category, namespace) {
...
...
namespace.returnedId = result.insertId; // set the value here

Then, use the function like this:

addToTable(myTitle, myContent, myFileName, myCategory, myNamespace)

Remember to pass a reference to myNamespace (referred to as namespace within the function). This way, you can access and update all attributes of myNameSpace.

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

Retrieving the current value of the selected option using JQuery

I am working on a feature where I have different quantities in selects after each button click. <button type="button" class="btn btn-sm btn-primary" id="addtocart2" >Button1</button> <select id="quantity1" class="ml- ...

Is it more effective to utilize a filter or request fresh data when filling in a form's drop-down choices?

Utilizing an axios ajax request, I am retrieving a JSON list of tags related to a specific topic selected from a dropdown. If no topic is chosen, all tags in the database (approximately 100-200 tags) are fetched. The process involves: User selects a topi ...

Error message: "An issue occurred with the Bootstrap Modal in

I've designed an AngularJS app like so: <!DOCTYPE html> <html ng-app="StudentProgram"> <head> <title>Manage Student Programs</title> <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2. ...

JQuery not functioning properly within a dynamically loaded AJAX section

After loading some pages with Ajaxtabs, I encountered an issue where jQuery and Owl Carousel were not functioning properly. The classes and styles from Owl Carousel were not being applied to the divs after loading parts of the page. Here is an example of ...

Using getStaticProps in Next.js to retrieve menu data and seamlessly integrate it into the layout

I have integrated Sanity.io as a backend for my project, specifically managing the data for my main menu in parent/children object arrays. While I am able to successfully fetch this data, I prefer to utilize getStaticProps to ensure that my site remains c ...

What is the best way to eliminate the input range in a React date range picker?

Here is an image illustrating a date range picker: https://i.stack.imgur.com/pwKaI.png I am looking to remove the labels for days before today and starting from today in the date range picker. How can I achieve this? Below is my code snippet: class Better ...

VSCode is experiencing issues with recognizing .env* files for markup purposes and is also failing to recognize .env.local.* files

Current Environment: Utilizing NextJs, React, and VsCode Noticing a problem with syntax highlighting in my VSCODE editor. I have installed the following extensions: ENV DotEnv As per suggestions, I updated my json configuration file as follows: " ...

Transforming a CSV file into JSON format using Gatsbyjs

I am currently exploring the possibilities of GatsbyJs and considering the utilization of the gatsby-transformer-csv plugin. You can find the documentation for this plugin here. I have got my hands on two CSV files exported from WordPress that I am eager ...

jQuery allows you to dynamically add a link that can remove a control in a unique

I am facing an issue with generating controls and removing them dynamically. I am trying to include a 'Link' that will trigger a function to remove the dynamically created control. The control and link are placed next to each other. Below is the ...

Exploring through a table using JavaScript

I am struggling to search a table within my HTML for a specific term. The code I have works to an extent, but it does not account for alternatives such as searching for "what is that" when I type in "What is that". Additionally, I need the script to ignor ...

After refreshing the page, the JQuery radio button will still be selected

In my form, a radio button triggers an additional text field based on user selection. To achieve this functionality, I am using the following jQuery script: $(document).ready(function(){ $('input:radio[name="accountType"]').change(function() ...

Step-by-step guide on how to display chosen values in a multi-select dropdown menu

How can I dynamically select values in a multi-select box based on an array using Vue.js? I've attempted a solution but it's not working as expected. Any suggestions or help would be greatly appreciated. <div id="app"> <select class="m ...

An item was shown on the HTML page

Having some trouble displaying the graph generated by my R function on the opencpu server. Instead of the desired plot, all I see is [object Object] in the HTML page. Below is the snippet of code from my AngularJS controller: var req = ocpu.rpc("plotGraph ...

Is the NPM package not being imported? How exactly is it being utilized?

mediacms-vjs-plugin is a unique plugin designed for Video.js. The MediaCmsVjsPlugin.js source code begins with: import { version as VERSION } from '../package.json'; import 'mediacms-vjs-plugin-font-icons/dist/mediacms-vjs-icons.css'; ...

Angular: Assigning a key from one variable to a value in another

I am currently facing a challenge with rendering a form on a page using ng-repeat, where the data for this form is dynamically fetched from a request. Within this data, there is a nested array called "categories" which contains IDs. I want to display the n ...

Retrieve the URL of an image located within an <li> tag within a jQuery selector item array

I am currently using a jQuery slider plugin to display images, and I am attempting to retrieve the URL of the current image when a button is clicked. The slider functions by showcasing all the image slides as list items in a continuous horizontal row. It ...

Discover the best approach for transforming a complicated JSON object/array into a map using inheritance coding techniques

Could someone help me with creating an array of objects (only 1 level) based on the following JSON structure? [ { 'family' : { 'name' : 'Doe', 'from' : 'Foo' }, ...

Change the default values for grid column configurations in Ext JS globally

The Ext.grid.column.Column class contains the following configurations: draggable (Default: true) sortable (Default: true) menuDisabled (Default: false) Is there a way to globally change the default values of these configurations for all grid columns i ...

Implementing Vue.js - Applying a Class on Click Behavior

Is there a way in Vue.js to dynamically toggle a class on a click event for a div without relying on properties or data? Take a look at this div: <div class="quiz-item" @click="checkAnswer(1)"> Upon clicking this div, I want to add either the clas ...

The Angular Material dialog fails to display content when triggered within an event listener in Google Maps

Within my project, I am utilizing Angular 6.0.6 and Angular Material 6.3.0. One issue I have encountered is with a dialog component that I have added to the entryComponents in the app module. Strangely, when I attempt to open this dialog within the rightcl ...