JavaScript server variable

How can I access a server-side asp.net variable in JavaScript? I'm trying to achieve something like this:

function setBookmark(val)
{
    document.getElementById('<%# hBookmark.ClientID %>').value=val;
}

Is there any way to accomplish this?

*Note*: hBookmark is a server-side HTML hidden control, and I want to use the client ID since IDs change as server controls are rendered.

Answer №1

// Remember to use <%= instead of <%#
update the value of the element with the id '<%=hBookmark.ClientID%>' to the variable 'val';

Answer №2

One way to incorporate scriptlets is by using the following code...

document.getElementById('<%= hBookmark.ClientID %>').value = val;

Keep in mind that the Javascript cannot be stored in an external file; it must be within the markup for it to be effective.

Answer №3

You have the option to assign the variable as a value for a hidden input field

<input type="hidden" value="<%= hBookmark.ClientID %>" id="hBookmarkClientID" />

After that, you can access it using JavaScript

function updateBookmark(value)
{
    var hBookmarkClientID = document.getElementById('hBookmarkClientID').value;

    document.getElementById(hBookmarkClientID).value = value;
}

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

"Incorporate countless Bootstrap 4 carousels into one webpage for an enhanced user

I'm experiencing difficulties with the new Bootstrap 4. Can anyone provide guidance on how to incorporate multiple Bootstrap carousels into a single page? I've researched several websites, but most only offer solutions for Bootstrap 3. Your assi ...

Utilizing npm packages with grunt: A guide

Initially, when I was working with node.js without grunt, I simply had to write the code below to import an external module. var express = require('express'); However, after transitioning to grunt, I attempted to utilize the qr-image module in ...

Cannot find a function within the Promise

Here is the code snippet I am working with: var c = function(address, abiJson){ var _ = this; this.data = { wallet: false, account:{ address: false }, contract:{ addre ...

ASP.NET - Unusual Glitch Found in Redirecting Pages

Working on a website project that involves user account login functionality. Once logged in, the user has the ability to update their secret question or answer. This process requires the user to go through three specific pages: 1) The initial page prompt ...

Manipulate the text within a canvas element using JavaScript

In this scenario, suppose json.mes represents the received message from Ajax. There is a button implemented to display the message in a canvas box. Although I have attempted to retrieve the message using getElementById (as shown below), it seems to be in ...

The initiation of the application in the Global file and IProcessHostPreloadClient interface

I have been trying to implement a start-up process in my ASP.NET application by following the steps outlined in this resource. Currently, we have a Quartz.NET scheduler registered in the Application_Start method of our ASP.NET application, as shown below: ...

Developing a custom library to enable Ajax capabilities in web applications

Currently, I am in the process of developing my own personal library. jQuery doesn't quite meet my needs, and I prefer having a clear understanding of what is happening within my code. Nevertheless, I'm encountering an issue with the ajax functio ...

Tips for organizing an array of objects in JavaScript according to a specific attribute value?

I'm trying to sort an array of notification objects in decreasing order of severity: Error > Warning > Information. For example: var notificationArray = [ { code : "103", severity : "Error" }, { code : "104", severity : "Inform ...

The development mode of NextJS is experiencing issues, however, the build and start commands are functioning normally

Just finished creating a brand new Next app (version: 12.0.7) using Typescript and Storybook. Everything seems to be working fine - I can successfully build and start the server. However, when I try to run dev mode and make a request, I encounter the follo ...

Update your mappings for the city of Istanbul when utilizing both TypeScript and Babel

Currently, I am facing the challenge of generating code coverage for my TypeScript project using remap Istanbul. The issue arises due to the usage of async/await in my code, which TypeScript cannot transpile into ES5 directly. To circumvent this limitation ...

Is it possible to create a popup window that remains fixed at the top edge of the screen but scrolls along with the rest of the page if

In an attempt to organize my thoughts, I am facing a challenge with a search page similar to Google. The search results trigger a popup window when hovering over an icon, located to the right of the search results. Here is what I am looking to achieve with ...

Pagination of AJAX result on the client side using jQuery

My issue revolves around pagination in AJAX. I want to avoid overwhelming my search result page with a long list of returned HTML, so I need to split it into smaller sections. The Ajax code I am currently using is: $.ajax({ url: "getflightlist.php?pa ...

Switch modules based on user options

My goal is to process an array of image files. I have the option to select them randomly or one by one (queue) based on the configuration specified in the config.json. To start off, I create the processor and determine the appropriate image selection modu ...

Obtaining the value of SCOPE_IDENTITY to be used in an Angular application

Having trouble getting the ID of the newly inserted table row to use in my Angular application. Any suggestions on how to pass it while posting? Can't quite figure it out. public string Post(Profile profile) { try { string ...

How to detect a right-click on empty time slots in Fullcalendar agenda views

I am working with a calendar feature on my web application using Adam Shaw's fullcalendar 2.1.1 JS library. I have successfully set up right-click responses for events and days by binding the "mousedown" event in the dayRender and eventRender callback ...

Generate Array of Consecutive Dates using JavaScript

My array contains the following values (for example): [ 1367848800000: true, 1367935200000: true, 1368021600000: true, 1368108000000: true, 1368194400000: true, 1368367200000: true, 1368540000000: true, 1 ...

Angular dependency injection function

What is the best placement for the common handleError and handleSuccess functions? These functions are commonly used by every service. Where should these functions be placed? Should they be global functions injected as dependencies? (function () { "u ...

When trying to run a jQuery function on click or load events, an error message stating that

When I have an .on(click) event triggering an ajax call, I want the same actions to occur when the page loads as well. My idea is to create a function that contains everything within the .on(click) event and trigger this function on page load. Although I ...

What could be causing the consistent Mocha "timeout error" I keep encountering? Additionally, why does Node keep prompting me to resolve my promise?

I'm encountering a timeout error repeatedly, even though I have called done(). const mocha = require('mocha'); const assert = require('assert'); const Student = require('../models/student.js'); describe('CRUD Tes ...

Adding a class in Javascript if its parent element already has a class

I am trying to assign a second class to a div if one of its parent elements has a specific class. Here is the approach I took: var elements = document.querySelectorAll('h1,h2,h3,h4,p'); //SELECT ALL ELEMENTS for (var i = 0; i < elements.lengt ...