Welcome to WebmasterWorld Guest from 54.226.189.112

Forum Moderators: open

Message Too Old, No Replies

[Need Help] Javascript insert in hidden Form

     

oxsa

4:53 am on Sep 29, 2012 (gmt 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

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


This java script

<script >
function name () {
var name = [
"Doe",
"Ray",
"Me"
];
ranNumber = parseInt(name.length * Math.random());
return name[ranNumber];
}
</script>

MichaelBluejay

11:39 pm on Sep 29, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



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];
</script>


Notes:

(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).

oxsa

11:54 pm on Sep 29, 2012 (gmt 0)



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

Featured Threads

Hot Threads This Week

Hot Threads This Month