What is the best way to manage a string containing quotes?

Currently, I have a JavaScript function that is being called using the following code:

 <a href='javascript:void(0)' onclick='javascript:onEditRevPrepare("   <%#Convert.ToString(Eval("ReviewTitle"))%>">

The issue arises when the Eval("ReviewTitle") contains a single quote or double quote, causing the function to not execute properly.

I suspect that the problem lies in this line of code:

Convert.Tostring(....)

However, I still need to convert it into a string before passing it to the JavaScript function.

How can I resolve this issue?

This is the JavaScript function in question:

function onEditRevPrepare(revTitle)
{

...
...
}

Answer №1

If you need to include double quotes in your code, you can try using the &quot; entity.

Check out the live demo here

onclick='javascript:onEditRevPrepare(&quot;<%#Convert.ToString(Eval("ReviewTitle"))%>&quot;'

Answer №2

Are you experiencing issues with specific values or is a function not being called at all? Please provide more details.

I believe the problem may not lie with " as long as the method is not being called. Give this a try and let me know if the issue persists.

<a href='javascript:void(0)' onclick='javascript:onEditRevPrepare(" + <%#Convert.ToString(Eval("ReviewTitle"))%> + ">

UPDATE:

I suspect the issue arises from not properly closing your function with ) when calling it within onclick, and also consider replacing any instances of ' with &#39;. The correct format should be as follows:

<a href='javascript:void(0)' onclick='javascript:onEditRevPrepare("<%#Convert.ToString(Eval("ReviewTitle"))%>).Replace("'", "&#39;")">

Thank you

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

Razor in CharJs

My app focuses on creating an Online Poll where users can manage questions and answers, and also vote on answers. Now, I want to visually represent the number of votes for each answer using a graphical chart. I came across the Chart.js API and decided to i ...

I am facing an issue where the module pattern functions perfectly on JSBin, but fails to work properly

As a backend developer, I typically only write JavaScript when absolutely necessary, and not always in the best way. However, I have decided to turn over a new leaf and start writing more organized code that follows best practices as much as possible. To ...

Is it possible to dynamically evaluate the fs argument?

When I tried to run npm run dev, I encountered the following error: An error occurred while attempting to evaluate the fs argument statically [0] 50 | // Read file and split into lines [0] 51 | var map = {}, [0] > 52 | content = fs.read ...

MVC Razor View does not display line breaks

Generated in the controller is a string value that looks like this: cc.lstchat = "Reply the Number with your question... <br />"; foreach (clsChat item in lstchat) { cc.lstchat =cc.lstchat + ite ...

Clone the "big" div element and insert data within it

Is there a way to dynamically generate div elements while looping through a JSON array? I have various data items that I need to display in separate thumbnails within a row. I am looking for a template structure like the following: for(var i = 0; i < ...

What function does the express res.get(field) method serve?

I came across some code snippets demonstrating the use of express res.get(field) Is this method primarily for debugging purposes or are there other applications? The sample code that caught my eye can be found at this link. Here is a snippet of it: var e ...

Using AngularJS to filter search results based on two user-provided input parameters

Currently, I am working on an Angular application that presents movies in a table format with search input and dropdown filter by genres. As I work on this project, my main focus is finding a way to filter search results based on both the text entered in t ...

What is the method for retrieving values from an object using keys that are subject to change?

Here is the code snippet I am working with: bodyLength.forEach((el, i) => { console.log(`${values.bodyTitleEn2 + i.toString()}`); body.push({ title: [ { key: 'en', value: values.bodyTi ...

Retrieving user input in Angular and showcasing extracted keywords

I want to give users the flexibility to customize the format of an address according to their preference. To achieve this, there will be a text input where users can enter both keywords and regular text. The goal is to detect when a keyword is entere ...

AngularJS's ng-click function seems to be malfunctioning

Currently, I am in the process of learning angularJS with the help of the book titled "Learning Web Development with Bootstrap and AngularJs." One challenge I am facing is creating a ng-click functionality for a button in my project. Unfortunately, when I ...

Adding Vuetify to a Vue web component, then proceed to pass props to enhance its functionality

Currently working on developing a web component with Vue and using vuetify component library. The goal is to export it as a web component, but facing difficulties when trying to pass props to the exported component in the index.html file. Following the ste ...

Guide on generating virtual nodes from a string containing HTML tags using Vue 3

Investigation I stumbled upon this solution for converting a simple SVG with one path layer into Vue2 vnode list and wanted to adapt it for Vue3. After scouring resources, including the MDN docs, I couldn't find any pre-existing solutions. So, I dec ...

JavaScript: Filtering list by elements that contain a certain value

I have the following collection of objects: [ { employeeId:1 employeeName:"ABC" }, { employeeId:2 employeeName:"ABD" }, { employeeId:3 employeeName:"FEW" }, { employeeId:4 employeeName:"JKABL" },] I am looki ...

Iterate through the form fields and save the information into an object

I am attempting to create a JavaScript object by looping through form inputs. const data = {}; // loop through each input found in form $('#form_name').filter(':input').each(function() { const $id = $(this).attr('id'); c ...

Retrieve selected value from Angular strap select dropdown

Angular strap is being used and the select element appears as shown below: <button type="button" class="btn btn-default" ng-model="selectedObject" data-html="1" bs-options="object.title for object in my_objects" bs-select> Action ...

Ajax was intended to introduce a session variable, however, it is not functioning as expected

My experience with AJAX is limited, but I am attempting to implement a seemingly simple functionality. I have a dropdown select form field that triggers a JavaScript function containing an AJAX call. <select onchange="update_session_value('123&ap ...

How can I retrieve the latest state of the Redux store in getServerSideProps in Next.js?

I am attempting to retrieve the most up-to-date redux state in getServerSideProps like so: export const getServerSideProps = async (ctx) => { const state = store.getState(); console.log(state.name); return { props: { login: false } }; }; H ...

Node.JS AJAX request with no data payload

Encountering an issue where no data is being received on the server side from an AJAX request because the req.body is empty (console logs it as {}). script.js: $("#button").click(function(){ var number = $("#number").val(); $.ajax({ ...

Struggling to efficiently handle imported JSON data using VUE.JS JavaScript?

Struggling to extract specific information from JSON data that I need to import. Here is the sample data I'm working with: I am trying to extract details like the name, description, and professor for each entry. This is how I'm importing the d ...

In Reactjs, it is important that each child in a list is assigned a distinct "key" prop

I am currently working on fetching data in Nextjs/Reactjs after clicking a button, but I am encountering the following error in my console: Each child in a list should have a unique "key" prop Below is my current code where data is displayed wit ...