0

I don't know if this can be do it, i have a input with a submit:

Variable JSON (data/data.links)
    <input type="text" id="newVarJson" value="data" onclick="verifica();"/><br>

and I need to put the value of that input as the name of a variable in javascript to create a json with that name, i thought on make that:

<script type="text/javascript">     

function verifica(){
    var document.getElementById(newVarJson).value={}
    data.value="asdf";
    alert(JSON.stringify(data));
}

</script>

As you see, the variable the input value = data, I don't know if this can be do it, so, can someone tell me any tips to make this, or it can't be do it?

Thanks!

8
  • 1
    quotes are important: .getElementById('newVarJson') ; also by assigning to value it is clearing the form value -- don't you want to read it instead? Commented Mar 28, 2014 at 9:53
  • that code it's not a child of a form, i don't use a form, only the tags input and the submit, it's a test that i need to implement into a project if it works Paul Commented Mar 28, 2014 at 9:57
  • I don't understand what " I need to put the value of that input as the name of a variable in javascript to create a json with that name" means. To create a string of json that represents a variable, you could use var myJsonString = JSON.stringify(myVar) Commented Mar 28, 2014 at 9:57
  • I meant, i have an input with the value = data, so can i assign that value as name of a variable on javascript? Commented Mar 28, 2014 at 10:00
  • I edited my comment with a working code :) Commented Mar 28, 2014 at 10:02

1 Answer 1

2

You can try this:

window[document.getElementById(newVarJson).value];

and your var name will be the value of that input.

Edit:

window[document.getElementById('newVarJson').value];

Working jsbin: http://jsbin.com/doparibu/1/edit

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

1 Comment

newVarJson should be a string, since it is the ID, so you should quote it. It is quoted in your jsbin.

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.