admin管理员组文章数量:1432601
I have this function getOrderNumber()
that I want to create a unique order number using the javascript getTime()
. I need then the function getOrderNumber()
to somehow assign a value to the html element. I tried a few iterations of what I have here which didn't work.
Any help would be appreciated.
<form action="/saocctest.cgi" method="post">
<!--get unique order number-->
<script>
function getOrderNumber() {
var d = new Date();
var n = d.getTime();
form.elements("orderNumber").value=n;
}
</script>
<input type="hidden" name="orderNumber">
I have this function getOrderNumber()
that I want to create a unique order number using the javascript getTime()
. I need then the function getOrderNumber()
to somehow assign a value to the html element. I tried a few iterations of what I have here which didn't work.
Any help would be appreciated.
<form action="https://www.someurl/saocctest.cgi" method="post">
<!--get unique order number-->
<script>
function getOrderNumber() {
var d = new Date();
var n = d.getTime();
form.elements("orderNumber").value=n;
}
</script>
<input type="hidden" name="orderNumber">
Share
Improve this question
edited Feb 8, 2023 at 5:53
Elijah M
87711 silver badges24 bronze badges
asked Sep 15, 2016 at 21:31
got2bgot2b
391 gold badge1 silver badge8 bronze badges
1
-
Seems like you need to do a bit more study. I'm not sure what you're really trying to acplish here. What's calling your function? Where did the variable
form
e from? Maybe this will help: w3schools./js/js_htmldom_html.asp – OneHoopyFrood Commented Sep 15, 2016 at 21:36
3 Answers
Reset to default 1Set Id name to your HTML form
<form id="myForm" action="https://www.someurl/saocctest.cgi" method="post">
And then assign the value using this in your function
document.getElementById("myForm").elements[0].value = 'hello';
Or else add id/class to your input and the assign using
document.getElementById('id'); // Using Id
document.getElementsByClassName('class')[0]; // Using class
It will work
Demo : https://jsbin./molagu/2/edit?html,js,output
JavaScript doesn't create global references of HTML page elements in this way. In modern browsers, if you've a element with specific identifier, you can directly get it from its id in the global object.
For example
<div id="my-id"></div>
JS
alert(this['my-id'])
Where this
, window
, if you're not in strict mode and if you're in a global scope.
Actually, if you want a cross-browser equivalent way to get the form
element,
document.getElementsByTagName('form')[0]
Every DOM element has methods like getElementsByTagName
, getElementsByClassName
, (these return a HTMLCollection
, looks like a array, but it's not) ...
The document
is the one object that contains the getElementById
method, which returns the first element with specific identifier.
Also, if you want to get your form elements, use the methods I quoted, or simply index the form reference with the 'name'
attribute of the specific child. It's cross-browser to use HTMLElement().getElementsByTagName
, etc.
Only input elements has the value
property. You can change the innerHTML
or innerText
instead (they're getters/setters);
I think this is what you're after
<form id="myForm" action="https://www.someurl/saocctest.cgi" method="post">
<script>
function getOrderNumber() {
var myForm = document.getElementById('myForm');
var d = new Date();
var n = d.getTime();
myForm.elements[0].value = n;
}
</script>
<input type="hidden" name="orderNumber">
Notice you could probably just give the input an id and change the value that way e.g.
<input type="hidden" name="orderNumber" id="myOrderNumber">
then
document.getElementById('myOrderNumber').value = n;
Also note when using document.getElementById("myForm").elements[0].value = 'hello';
as per other answers here that you are targeting the first element within the form, if you ever add another element above this then you will update the wrong item value.
Safest option as I've mentioned would be to place an id on the input itself then target that.
本文标签: Assign value to html element using javascriptStack Overflow
版权声明:本文标题:Assign value to html element using javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745605677a2665823.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论