Elisia Light
Elisia Light

Reputation: 115

How can I insert result of JS function in html form?

I have a question - how insert result of JS function in html form? I need to put time (in hh:mm format) into "value" in form. The JS code:

function getHours() {
var d = new Date();
var h = d.getHours();
var m = d.getMinutes();
document.getElementById('clientid').value=h+" "+m;}

The HTML form:

<html>
<form method='post' action='http.example.com/some.php'>
    <input type='hidden' name='clientid' id='clientid' value="test" action="javascript:getHours()" />
</form>
</html>

Thank you for your attention!

Upvotes: 0

Views: 1719

Answers (4)

ozil
ozil

Reputation: 7117

you can use onload() method to call your function getHours(), but .onload() support following tags:
<body>, <frame>, <iframe>, <img>, <input type="image">, <link>, <script>, <style>

Upvotes: 0

Beginner
Beginner

Reputation: 710

Try this

<script>
document.addEventListener("DOMContentLoaded", function(event) { 
	var name = "Beginner";
	document.getElementById("clientid").setAttribute("value", name);
});
</script>

<input name='clientid' id='clientid' />

Upvotes: 0

JGCW
JGCW

Reputation: 1529

$time = h + ':' + m ;
$('#clientid').val($time);

the $time = h + ':' + m ; adds the hours and minute to the format you want to the $time variable.

After that the $('#clientid').val($time); tells JS to add that variable to the clientid 's value.

Upvotes: 0

Shota
Shota

Reputation: 7330

You need to load your function like this:

<html>

<body onload="getHours()">
    <form method='post' action='http.example.com/some.php'>
        <input type='hidden' name='clientid' id='clientid' value="test" action="javascript:getHours()" />
    </form>

<script>
    function getHours() {
        var d = new Date();
        var h = d.getHours();
        var m = d.getMinutes();
        document.getElementById('clientid').value = h + " " + m;
    }
</script>
</body>

</html>

Upvotes: 2

Related Questions