crazyoxygen
crazyoxygen

Reputation: 716

How can I pass value from javascript to html?

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 ??

Upvotes: 0

Views: 2971

Answers (2)

Manse
Manse

Reputation: 38147

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

Upvotes: 2

Rene Pot
Rene Pot

Reputation: 24815

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

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

Upvotes: 6

Related Questions