/    Sign up×
Community /Pin to ProfileBookmark

Create a random number in the value= attribute on a hidden input field

I’m wanting to have a randomly generated number as the value of a tag each time I reload the page.

<form action=”whatever.asp” method=”post” name=”myform” onsubmit=”return ValidateForm(this)”>

<input type=”hidden” name=”v46″ value=”8773″>

</form>

I want to replace the [B]8773[/B] of the attribute value=”8873 with a randomly generated number between 1-9999. So far I have found the code below which generates a random number as the value in a visible input field.

What I need it to do is change in the actual code of the page each time the page reloads for a [B]hidden input value[/B]. So evertime you refresh the page the value=”8873″ in the actual code changes to a different number.

The JavaScript I have so far is

[CODE]
<script type=”text/javascript”>
var n=9999;
onload=function(){
var rand = Math.ceil(Math.random()*n);
document.forms[0].elements[‘v46’].value=rand
}
</script>
[/CODE]

Any Ideas? Any help would be greatly appreciated.

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@emoritzAug 14.2006 — the following code works:

[code=html]
<script type="text/javascript">

var n=9999;

window.onload=doRand;

function doRand() {

document.forms[0].elements['v46'].value = 0;
var rand = Math.ceil(Math.random()*n);
document.forms[0].elements['v46'].value=rand;

}
</script>

<form action="whatever.asp" method="post" name="myform" onsubmit="return ValidateForm(this)">

<input type="hidden" name="v46" value="8773">
<input type="button" onclick="alert(document.forms[0].elements['v46'].value);" value="Click to see value">

</form>
[/code]


you must assign a new value to v46 (in this case, 0) on each page refresh else it retains the previous' page value
×

Success!

Help @infovision spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...