Welcome to WebmasterWorld Guest from

Forum Moderators: open

Message Too Old, No Replies

[Need Help] Javascript insert in hidden Form

4:53 am on Sep 29, 2012 (gmt 0)

New User

5+ Year Member

joined:Sept 28, 2012
posts: 2
votes: 0

Hi guys, need help

i've script Math.random, i need call function name random if browser/page refresh and i'need insert value name on hidden input form by ID name , how do that ? please help me i'm newbie :(,
thanks guys ..


<form metod="POST">
<input id="email" type="text" />
<input id="name" name="name" type="text"/>
<input type="submit" value="send">

This java script

<script >
function name () {
var name = [
ranNumber = parseInt(name.length * Math.random());
return name[ranNumber];
11:39 pm on Sept 29, 2012 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month

joined:Feb 12, 2003
posts: 1208
votes: 0

Your HTML is okay. Here's the Javascript you need:

<script > 
names = ["Doe", "Ray", "Me"];
now = new Date();
seed = now.getSeconds();
ranNumber = Math.floor(Math.random(seed)*3);
document.getElementById('name').value = names[ranNumber];


(1) You don't need a function, since you want the script to run just once when the page loads.

(2) The first two lines make sure you get a random number every time.

(3) Do NOT use the word "name" as the name of a variable! It's a reserved word in Javascript.

(4) For that matter, it wouldn't hurt to change the id of the <input> to "nameField" or "name_" or something (and then change it in the script, too).
11:54 pm on Sept 29, 2012 (gmt 0)

New User

5+ Year Member

joined:Sept 28, 2012
posts: 2
votes: 0

@MichaelBluejay : thank you brother
it worked, I was helped ... :D