Can a JavaScript function accept a variable as a parameter?

In the code snippet below, I have a function that handles the event when a treenode is changed:

 protected void mytv_SelectedNodeChanged(object sender, EventArgs e)
 {
    TreeView tv = sender as TreeView;
    var nid = tv.SelectedNode.Value;
    ScriptManager.RegisterStartupScript(this, this.GetType(), "anyname", "show(nid)", true);
}

The variable nid in the code snippet above captures different values (1,2,3) based on the clicked treenode.
Is there a way to directly pass this nid value as a parameter to the show() javascript function?
If not, how should I handle this scenario?

Here is the definition of the javascript function mentioned in the code snippet:

function show(nid) 
{
    if (nid == 4) {
        alert('testing');
    }
    else
     {
         alert('what???');
     }
}

Answer №1

Here is a tip for you to try: concatenate like this:

ScriptManager.RegisterStartupScript(this, this.GetType(), "anyname", "show(" + nid + ")", true);

Answer №2

// Displaying string value
ScriptManager.RegisterStartupScript(this, this.GetType(), "uniqueID", string.Format("display('{0}')", id), true);
// Displaying integer value
ScriptManager.RegisterStartupScript(this, this.GetType(), "uniqueID", string.Format("display({0})", id), true); 

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

Connecting a dynamically assessed function on the $scope to a service

Within my code, there is a function called make_value_summary that has the capability to generate a generic summary of various fields within the $scope. By applying this function, I am able to create location_summary, which is then linked to the view using ...

A guide on verifying discrepancies between selected record data in two tables

How can I validate if the data of selected records is mismatching in two tables? I need to select a few records in each table. Upon clicking the Match button, if the currencies are not the same, we need to display an alert saying "Data mismatched". I have ...

AngularJS: The requested resource does not have the "Access-Control-Allow-Origin" header

Currently developing my web application using AngularJS, I have a file named script.js which contains the following code: var module = angular.module('project', ['ngRoute']); // setting up our routes module.config(function ($r ...

Using Three.js to import and cast rays on a .obj model created in Blender

I have successfully imported a 3D terrain using Blender and the OBJLoader in Three.js. In addition, I have created a mesh (highlighted in yellow in the image below) that I want to follow the mouse cursor while it hovers over the terrain. I have attempted t ...

Steps for deploying a website and a web API on a single VM

I am currently using an Azure VM for hosting a default website, and I now have a separate Web API application that I want to deploy on the same VM in order to use the API for a mobile app. Is it possible to deploy the Web API on the same VM? Any help woul ...

Concerns regarding code coverage when testing asynchronous functions using nockjs and jest

In my latest project, I've created a basic unit test for making API calls using NockJS and Jest within a React application. Here's a snippet of the code: AjaxService.js export const AjaxService = { post: (url, data, headers) => { ...

When the button is clicked, update the text within the <script> tags utilizing jQuery, JavaScript, or PHP

Here is the code snippet where changeme represents the text that needs to be replaced upon clicking a button: The following HTML and JavaScript code demonstrates this functionality: 1) Any input provided by the user in the field will be captured, replaci ...

Produce a variety of shapes using Three.js

I am looking to generate multiple objects. var distance = 10; var geometry = new THREE.BoxGeometry(10,10,10); var material = new THREE.MeshBasicMaterial({color:0x00ff44}); for(var i = 0; i < 4;i++){ var mesh = new THREE.Mesh(g ...

Issue with the positioning of the datepicker is not functioning properly

I'm currently facing an issue with the position of a date picker plugin from jquery. The search box on my website allows users to select a range of dates using the date picker, but when enabled, the date picker appears at the bottom left corner of the ...

We regret to inform you that the current version of ChromeDriver is not compatible with Chrome version 98, thus

Encountering this issue: Error: session not created. This version of ChromeDriver only supports Chrome version 98. Current browser version is 100.0.4896.75. Despite manually downloading the correct Chrome driver and placing it in the project's debug ...

Update the icon within the expandable display

Is there a way to change the plus sign to a minus sign in an expandable table view on click? (changing fa fa-plus to fa fa-minus) The goal is to have the plus sign displayed when the view is compressed and the minus sign displayed when it is expanded. If ...

unable to proceed with hosting on Vercel due to issues with the building process

My current setup involves using node.js as a server-side to store the API response. Everything works flawlessly, but when attempting to host it on Vercel, I encountered numerous issues. The project hit a roadblock during the building process... The output ...

npm encountered an error with the dependency "fsevents" as it was found to be empty

Encountering an issue while trying to install packages using npm. Seeking assistance to resolve it. premnath@premnath-Inspiron-5559:~$ sudo npm install -g @angular/cli [sudo] password for premnath: npm ERR! Object for dependency "fsevents" is em ...

Exploring the versatility of combining CSS classes with MUI 5 SX prop

Is there a way to use multiple CSS classes with the MUI 5 SX prop? I've defined a base class for my Box components and now I want to add a second class specifically for styling the text inside the Box. When I try to apply both classes like sx={styles. ...

Trouble with jQuery delay in updating the CSS attribute while using fadeIn

After writing a simple JQuery code, I noticed that every time I click on 'eat', the animation lags. Is there any way to preload this animation for smoother performance? The #custom_menu element is a full-page section with a fixed position (simil ...

How to pass GetServerSideProps into Page component props in Next.js with custom _app.js

Having issues integrating GetServerSideProps into my Next.js app. Despite the network call successfully fetching data and generating the pageName.json, the response data is not being injected into the page props as expected. Below is a snippet of my page ...

What is the best method for transferring form data to a string and storing it in localStorage effectively?

Is this the most efficient method for extracting form data into a string and storing it in localStorage? I developed this method independently, but I'm not an experienced programmer. It seems to work for my needs, but I'm unsure if it's com ...

"Enhancing Collaboration with NextJs Multi-Zones' Unified Header

Currently, I have two applications named admin-shell and delivery-management, both of which are being managed using Multi Zones in NextJs. These applications share a common header with navigation links, but I am encountering difficulties navigating betwee ...

Execute the (click) functions at specified intervals using a Jquery loop

My code is designed to change the background position on click, but I also need it to loop automatically. <script type="text/javascript> $(window).load(function(){ $(".sle1").click(function() { $(".slimgs").animate({backgroundPosition: & ...

Step-by-step guide: Adding a Google Maps API key to your WordPress theme

Having an issue with Google Maps on my WordPress website. The error message displayed is: Google Maps API error: MissingKeyMapError I have obtained a Google Maps API key, but I am unsure where to insert it. I am not using a Google Maps plugin; instead, my ...