giles
giles

Reputation: 1

return js value in HTML

First tentative steps into client side. I'm having trouble finishing the following. My question is how do I return the value of a function in a HTML statement ...

<script language="Javascript">
function checkjava()
{
 return 1
}
</script>

</head>
<body>
<form action="enabled_catch.php" method="get" name="your_form">
<input type="HIDDEN" name="answer" value="RETURN checkjava() HERE")>
<input type="submit" value="click me">
</form>
</body>

I'd appreciate your help Thanks in advance

G

Upvotes: 0

Views: 3837

Answers (6)

ase
ase

Reputation: 13471

<head>
  <script language="Javascript">
    function checkjava() {
      return 1
    }
  </script>
</head>
<body>
  <form action="enabled_catch.php" 
        method="get" 
        name="your_form"
        onsubmit="this.answer.value=checkjava();">
    <input type="hidden" name="answer">
    <input type="submit" value="click me">
  </form>
</body>

No need for the id attribute.

Upvotes: 2

Arshdeep
Arshdeep

Reputation: 4323

<form action="enabled_catch.php" method="get" name="your_form" >
<input type="HIDDEN" name="answer" value="RETURN checkjava() HERE")>
<input type="submit" onclick="document.getElementById('answer').value=checkjava();"  value="click me">

Upvotes: 1

Ibrahim AshShohail
Ibrahim AshShohail

Reputation: 2192

<script>
document.your_form.answer.value = checkjava();
</script>

Unless you want to use a fancy JS library. =)

Upvotes: 0

Daniel Vassallo
Daniel Vassallo

Reputation: 344351

Browser-side scripting is event driven. Unless an event is triggered, nothing happens.

You could listen to the click event on your submit button, and then manipulate the value of your hidden field:

<input type="hidden" id="answer" name="answer" value="")>
<input type="submit" value="click me" 
       onclick="document.getElementById('answer').value = checkjava();">

Note how we had to give an id to the hidden field, in order to be able to reference it with getElementById().

Also note that JavaScript is case sensitive: checkjava() and checkJava() are not the same.

Upvotes: 0

Adam Butler
Adam Butler

Reputation: 3037

I would have an id on the answer tag and then do it in the form onsubmit event.

Something like:

<form onsubmit="document.getElementById("answerId").value = checkJava()" action="enabled_catch.php" method="get" name="your_form">
<input type="HIDDEN" name="answer" id="answerId" value="RETURN checkjava() HERE")>
<input type="submit" value="click me">
</form>

Upvotes: 0

Strelok
Strelok

Reputation: 51461

You would do something like that:

<script language="Javascript">
  function checkjava()
  {
   return 1
  }
</script>

</head>
<body>
 <form action="enabled_catch.php" method="get" name="your_form" onsubmit="document.getElementById('answer').value=checkjava();">
  <input type="HIDDEN" id="answer" >
  <input type="submit" value="click me">
 </form>
</body>

Basically on form submit you would set the value of the answer input to the result of the function call.

EDIT: placed onsubmit on the wrong element before (embarrassing).

Upvotes: 0

Related Questions