Skip to content
Advertisement

how to write JavaScript object in the textarea(HTML Tag) to get that object by using documentQuerySelector

I wanna write javascript object in Textarea or input field and wanna retrieve those data by using documentQuerySelector. but when I do this it returns a string.

IS there any way to write javascript code in the browser and access that code form my javascript file like app.js

HTML

JavaScript

I use Event for the value In react

JS / REACT I

JavaScript

the object that i write in the text area

JavaScript

but when i retrieve the data and console.log it gives string instead of js object

JavaScript

Advertisement

Answer

The event.target.value for textarea in this case will return you a string

If you want the object, you must use JSON.parse(textValue) to get it

Moreover, the textValue passed to JSON.parse must be a valid JSON (you could look at the MDN doc for more detail on what a valid JSON would look like)

For example, to get it, you could declare an object and use JSON.stringify(obj) to get the valid JSON.

JavaScript
JavaScript

After having a valid JSON string, paste it in the textarea, and when you get the value from textarea, just JSON.parse() that value to get your object value

JavaScript

Edit cranky-shape-7hggw

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement