0

I want to give the java script object structure as text input into the inputTextarea and i want a var to read that as object. My object structure is like below. Instead of giving this in the inputTextarea i can give this directly in my xhtml page as var obj={ onchange: ... but that i don't want. Because there are difficulties in parsing the xml tags also, else it will throw some tag errors. Is that possible to read the text from inputTextarea as var object instead of reading as string .

{
onchange: function(){
console.log("I been changed now!")
},
validate: function(obj){
console.log("I be validatin' now!")
},
elements: {
"list": {
menu: [{
caption: "Append an <item>",
action: Xonomy.newElementChild,
actionParameter: "<item/>"
}]
},
"item": {
menu: [{
caption: "Add @label=\"something\"",
action: Xonomy.newAttribute,
actionParameter: {name: "label", value: "something"},
hideIf: function(jsElement){
return jsElement.hasAttribute("label");
}
}, {
caption: "Delete this <item>",
action: Xonomy.deleteElement
}, {
caption: "New <item> before this",
action: Xonomy.newElementBefore,
actionParameter: "<item/>"
}, {
caption: "New <item> after this",
action: Xonomy.newElementAfter,
actionParameter: "<item/>"
}],
canDropTo: ["list"],
attributes: {
"label": {
asker: Xonomy.askString,
menu: [{
caption: "Delete this @label",
action: Xonomy.deleteAttribute
}]
}
}
}
}
}
1
  • Can you take a moment and properly tab your code? Commented Jan 16, 2019 at 14:03

2 Answers 2

1

As far I understand you need to read a textarea content as a JavaScript object.

If the textarea content is a valid JSON, you can transform the textarea content into by calling JSON.parse.

If the textarea content is not a valid JSON, but some kind of arbitrary JavaScript object representation, you can use eval to transform it into a JavaScript object. However, take into account that using eval is a security concern, you should not use it unless the input is coming from a trusted source.

const textArea = document.getElementById('demo');

// this will work only if textarea content is a valid JSON
// const myObject = JSON.parse(textArea.textContent);

// this following is insecure, use on your own risk
const myObject = eval(`(${textArea.textContent})`);

console.log(typeof myObject);
console.log(myObject);
<textarea id="demo">
{ a: 1 }
</textarea>

Sign up to request clarification or add additional context in comments.

2 Comments

It is working fine in console. But in the original code it looks like eval(()) while inspecting instead of eval((${mytext})) . Why this missing occurs while running?
I have checked your answer, glad that you have figured it out. Looks like the target environment does not support template literals, that would explain why string concatenation solved the issue.
0

Solved the problem with

eval('(' + myinputtext + ')');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.