0

I have javascript function that get JSON from another php file but I can't pass value to html file. How can I do it ??

here plese take a look

<body>
    <script type="text/javascript" src="jquery.min.js"></script>
    <script type="text/javascript">
    $.getJSON("getquestion.php",function(result)
    {
        document.getElementById("question").innerHTML = result.test_question;   
        testform.textquestion.value = result.test_question;

    });
    </script>
<form name="testform"> 
<div id="question"></div>
<input id="aaa" type="text" name="textquestion"></input>

</body>

div:question can show data from result.test_question but input:aaa can't.

Could you please teach me how can I pass value to or ? and Can I assign name of function .getJSON and how ??

crazyoxygen
  • 706
  • 1
  • 11
  • 30
  • 1
    http://stackoverflow.com/search?q=%5Bjavascript%5D++pass+value+from+javascript+to+html, 3829 results – KooiInc Jul 27 '12 at 09:32

2 Answers2

6

Use jQuery properly since you're already using jQuery anyways.

$("#question").html(result.test_question);
$('#aaa').val(result.test_question);
Esailija
  • 138,174
  • 23
  • 272
  • 326
Rene Pot
  • 24,681
  • 7
  • 68
  • 92
2

Change

testform.textquestion.value = result.test_question;

to

document.getElementById('aaa').value = result.test_question;

or

document.testform.textquestion.value = result.test_question;

Docs for accessing forms using JavaScript here

or as you have linked jQuery .. you could replace

document.getElementById("question").innerHTML = result.test_question;   
testform.textquestion.value = result.test_question;

with

$("#question").html(result.test_question);   
$('#aaa').val(result.test_question);

Docs for .html() here and .val() here

Manse
  • 37,765
  • 10
  • 83
  • 108