Is there a way to retrieve a value with double quotes in JavaScript?

In my JavaScript code, I am trying to retrieve a variable value enclosed in double quotes. The value is obtained from an input field, but it comes wrapped in single quotes.

I have already attempted the following code:

let id=document.querySelector("#v_id");   //v_id represents the ID of the input field
let stringID= id.value;

var vObj = {id:stringID};

However, the result appears like this { id: '1'} with single quotes around the value.

This format does not work when using the value in a JSON object.

I require the result to be formatted as { id: "1"} with double quotes enclosing the value.

Your valuable response would be highly appreciated.

Answer №1

To convert data to a JSON string, you can use the JSON.stringify method.

console.log(JSON.stringify('123123'))

Answer №2

Converting the value is not necessary in this case. Instead, you can simply use bracket notation when assigning a value to the key

jsonObj[id.value] = 'someValue';

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

Build a Docker container for a project that requires utilizing yarn link for dependencies

While working on my NextJS project, I made the decision to utilize yarn as my package manager and utilized yarn link for import aliases/absolute imports. This feature of yarn is quite handy and is recommended for managing aliases within a project. However, ...

A guide on extracting JSON data in PHP and converting it into a string

Dear Everyone, I have a string retrieved from an API that contains data in JSON format. I am looking to separate and extract useful information such as the title, description, and URL from this string. The example output is as follows: {"PR":{"Url":"", ...

Utilizing JQuery to Transform Cell Content

I am utilizing jQuery and the CSVtoTable plugin to convert large CSV files into tables for manipulation. Each row needs to be associated with relevant links. One challenge I am facing is converting text in a row to include a link to a PDF. The issue lies ...

Ways to address a header in a table while scrolling

My table's thead section contains 2 tr elements with different colspans and rowspans, as shown in the image below: https://i.sstatic.net/5aLJO.png <table id="header-fixed"> <thead> <tr id="tr1" role="r ...

Having trouble with input event listeners in JavaScript?

I've been attempting to trigger the keyup event using EventListener for an input tag, but it doesn't seem to be working and I'm unsure why. Here is the code I have: document.getElementById("ajax").addEventListener("keyup", function() { ...

Press a button to generate an image inside a specified div element

I've been struggling with this particular issue and have attempted various solutions, but there seems to be an error in my implementation as it's not functioning correctly. My goal is simple: I want to be able to click a button and have an image ...

AngularJS: Populating the dropdown menu with selected data in ui-select

I am currently utilizing Angular.js version 1.3.4 and implementing ui-select within my project. My challenge lies in the process of binding data back to the ui-select component and a textbox after saving the information to the database successfully. Even ...

What are the steps to create a "load more" button that displays additional rows?

Struggling to get the code right for my webpage. I have a layout with 6 rows, each containing 3 columns filled with images. However, when I click on the "load more" button, nothing happens. I've attempted to modify the jQuery code from .slice(0, 3) t ...

Using keyvalue pipe in Angular to iterate over objects through inserted loops

My Customized Answer import { Question } from './question'; export class Answer { AnswerId: number; Content: string; IsCorrect: boolean; Mark: number; QuestionId: number; Question: Question; } My Personal TestStartComp ...

Having trouble with Wookmark not working in Jquery UI?

Hey there, just wanted to share that I'm currently utilizing the Wookmark jQuery plugin $.post('get_category',data={type:'All'},function(data) { $.each(data,function(item,i){ var image_ ...

Exploring JavaScript physics within a two-dimensional environment

I've been diving into learning Canvas (HTML5) on my own and I've managed to code most of a simple game engine. It's based on a 2D space theme with planets, stars, and celestial bodies. In my default "Sprite" class, there is a frame listener ...

Using Vue.js: How to Trigger a Child Method from the Parent Component

I recently explored the communication between components using vuejs and vuex. Here is a scenario I encountered: The first component is a menu The second component, nested within the first, is a hamburgerButton. In this specific case, when I click the ...

AJAX working concurrently across multiple threads

After learning that JavaScript is single-threaded (as discussed in this question: If Javascript is not multithreaded, is there any reason to implement asynchronous Ajax Queuing?), I started to explore how this concept applies to my developed application. H ...

Step-by-step guide on accessing a specific object in a MongoDB array using its ObjectID

I have an array of reviews and I am trying to extract a specific review from an array of objects within a schema. Below is the schema structure: const sellerSchema = new mongoose.Schema({ user: { type: mongoose.Schema.Types.ObjectId, ref: " ...

Ways to center align text in a div vertically

I'm working with 2 divs that are floating side by side. The left div contains a part number, which is only one line. The right div holds the product description, which can span multiple lines. I am looking for a way to vertically align the text in t ...

Creating a JSON array in JavaScript: A step-by-step guide

Presently, I am engrossed in a project involving the creation of a Box and Whisker Plot. The data for this task is being sourced from a PHP file and then passed on to a JavaScript function for the actual Box and Whisker Plot generation. In my possession, t ...

What is the best way to transform JavaScript into TypeScript?

I've been researching TypeScript extensively, but I'm still unsure about what type I should set for elements X and Y. Can someone help me understand how I can convert this code to TS? Thank you in advance for your assistance. const x = document.g ...

Is there a way to reduce the size of a div within another div using CSS?

Trying to resize a nested div (divB) within another div (divA) is proving challenging. The issue arises when the height of divA is determined by its content. When divB is resized, divA's height remains unchanged... This occurs due to the application o ...

What is the best way to delete the onclick event of an anchor element?

Struggling to remove the onclick attribute using jQuery? Here's my code: function getBusinesses(page){ if(page==0){ alert("you are already on First Page"); $("#previous a").removeAttr("onclick ...

Having trouble aligning two contact forms side by side in the center

I've been researching CSS techniques for hours, experimenting with different methods to align them horizontally, but I'm struggling to achieve the desired result. Currently, I am delving into the world of UI/UX design and finding inspiration on ...