My goal is to create a JSON object using input variables from two fields. Upon clicking a button, I want to capture the input values and use them to construct a JSON object accordingly. However, I am encountering issues with successfully creating the JSON object with the entered values.
Initially, I attempted the following:
// Fetching the input values as variables
let id=document.querySelector("#id");
let title=document.querySelector("#title");
Firstly, I tried the code snippet below. Unfortunately, it resulted in an empty value rather than the expected string:
var vObj = '{"id":' + id.value + ',"title":' + title.value + '}'
Another approach I experimented with was:
var vObj = {"id":{}, "v_title":{}, "v_category":{}};
vObj.id = id.value;
vObj.title = title.value;
Furthermore, I tested the following code snippet:
let asd = id.value;
let bsd = title.value;
var vObj = {id:asd, title:bsd}
Unfortunately, this also returned an empty value instead of the desired variable value.
Upon further investigation, I discovered that there was a typographical error in the button trigger which may have been causing these issues. My apologies for overlooking this mistake.