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 ??
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 ??
Share Improve this question asked Jul 27, 2012 at 9:28 crazyoxygencrazyoxygen 7161 gold badge11 silver badges31 bronze badges 1- 1 stackoverflow./…, 3829 results – KooiInc Commented Jul 27, 2012 at 9:32
2 Answers
Reset to default 6Use jQuery properly since you're already using jQuery anyways.
$("#question").html(result.test_question);
$('#aaa').val(result.test_question);
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