Tips for sending a string variable into a JavaScript function

Here is the code snippet I am working with:

JSP :

String userId = rs.getString("user_id");

<center><a href="#" onclick="edit(\'' + userId + '\');">Edit</a></center>

JavaScript :

 function edit(id)
 {
         alert(" user id is "+id);

 }

Unfortunately, this code is not functioning as expected.

An error is being thrown indicating:

Uncaught SyntaxError: Unexpected token ILLEGAL

Answer №1

It is essential that you follow these instructions

let userIdentifier = '${userIdentifier}'   <-- using ${} allows access to the variable.

Alternatively:

... onclick="modify('userIdentifier')" ...


function modify(userIdentifier) {
    let identifier = rs.getString(userIdentifier);
    alert('the identifier is: ' + identifier);
}

Warm regards.

Answer №2

hi buddies, feel free to utilize the following code snippet

<center><a href="#"  onclick="edit(  <%=userId %>  ;">Edit</a></center>

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 [object Object] in Angular Material datatable

I am currently working on implementing a datatable component using Express and Firebase DB. Below is the service request data: getText() { return this.http.get<nomchamp[]>(this.url) .map(res => { console.log(res); return res }); ...

Should alert boxes and loading windows be called from Controllers or Services?

As I work on developing an AngularJS (Ionic) app, I have come across many resources emphasizing the best practices in AngularJS development. One common guideline mentioned is to avoid using controllers for DOM interactions. I currently have calls to ioni ...

No value being returned by promise

Encountering a node issue with the use of the imap module, where the goal is to implement a promise. I have set up a Promise.method() using bluebird. The code can be found at: https://github.com/dkran/email-thinky-test/ The problem arises in a file that c ...

Creating a interactive navigation bar with External JSON data in HTML and JavaScript

Is there a way to create a dynamic MenuBar by using an external JSON file? How can I write HTML code to fetch and display JSON data dynamically? What is the process for reading a JSON file? //JSON File = Menu.Json {"data": [{"id": "1", "text": "F ...

Using discord.js to conveniently set up a guild along with channels that are equipped with custom

When Discord devs introduced this feature, I can't seem to wrap my head around how they intended Discord.GuildManager#create to function. How could they possibly have expected it to work with Discord.GuildCreateOptions#channels[0], for instance, { ...

Exploring innovative designs for asynchronous JavaScript programming

Imagine you have an Express app and you need to retrieve data from a database to display on the frontend. There's a function in your code that looks like this (using node-mysql for handling database queries) exports.getData = function() { ...

Building a multilingual website using AngularJS UI-Router

I am currently working on developing a multilingual website using AngularJS. While providing translations in templates seems straightforward, I am facing challenges when it comes to implementing proper multilingual routes using UI-Router. Unfortunately, I ...

Using jQuery to enable scrolling and dynamically changing classes

I'm currently working on enhancing my website with a feature that changes the opacity of the first section to 0.4 when a user scrolls more than 400 pixels down the page. I attempted the following code without success: if($("html, body").offset().top ...

Unraveling a SQL query result using JavaScript - the ultimate guide!

After exploring related links and conducting a Google search, I unfortunately haven't come across the exact solution to my issue. Apologies for being new to web development, but here's my question: I am working on an ajax JavaScript function tha ...

Kentico's reCaptcha feature does not prevent the submission of forms

My WebPart includes the reCaptcha FormControl. <script src='https://www.google.com/recaptcha/api.js'></script> ...... <div class="form-group"> <label>Please type the word below. If required use the buttons to ...

Caution: It is important that every child within an array or iterator is assigned a distinct "key" property

As a new developer in ReactJS, I have created a table using ReactJS on the FrontEnd, NodeJS on the BackEnd, and MySQL for the database. My goal is to retrieve data with a Select request on the table. For my frontend: class ListeClients extends Component ...

The latest version of Material UI icons caused compatibility issues with React-Select

I recently made the switch from using @material-ui/icons version 4.11.2 to @mui/material and @mui/icons-material version 5.2.3 Although material UI is not directly integrated with react-select, there seems to be some interaction happening. The transition ...

In order to ensure JavaScript can be universally applied to all images, it needs to be made more generic

I have developed JavaScript functions to enable zoom in and zoom out functionality for an image through pinching gestures. Now, I aim to refactor the code below so that I can include it in a shared JavaScript file. var scale = 1; var newScale; ...

Having trouble with Java's String.startsWith() method not recognizing the first line of a text file?

Looking for assistance with parsing information from a text file similar to the format below: #title キミと☆Are You Ready? #artist トライクロニカ #mobile deresimu #easy 0 #normal 22 #hard 27 #tag SHOW BY ROCK!! #preset all I have utilized t ...

disabling a button in React JS when multiple checkboxes are checked

I am facing a new task of creating a button that is enabled when a check box is checked and disabled when unchecked. My initial success with this was through the following code snippet. import React from "react"; import "./styles.css"; ...

A step-by-step guide on including chess.js in your Create React App

I'm facing an issue while trying to incorporate the chess.js npm library into my Create React App. The error message "Chess is not a constructor" keeps popping up. Below is the code snippet I am using: import React from 'react'; import &apos ...

Repeating calls to the init() function within an AngularJS controller

Encountering an issue while developing an angularjs application. I have set up a controller and bound it in routeProvider with a function to initialize values like so: angular.module('app.Login', ['app.rpc','ngRoute']) ...

Constructing a designated area for rest and rejuvenation

After adding the android-support-v7-appcompat library to my project, I encountered an error message: "java.lang.NoClassDefFoundError: android.support.v7.appcompat.R$styleable " To resolve this issue, I right clicked on the android-support-v7-appcompat an ...

Unable to invoke function within class using 'this'

I've been working on a class named Scheduler that runs a cron job using the cron module. I've defined a function to calculate the difference in days between two dates, and it works fine when called outside of the cron job iteration. However, if I ...

When you attempt to upload an MP3 file to a NodeJS Express server, the file becomes

I am facing an issue with uploading an MP3 file from my web client to a Node Express server. The uploaded file gets corrupted and I am unable to play the song using an MP3 player after writing it to disk. The problem seems to occur when uploading the file ...