homepage Welcome to WebmasterWorld Guest from 54.196.159.11
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
[Need Help] Javascript insert in hidden Form
oxsa




msg:4501457
 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




msg:4501785
 11:39 pm on Sep 29, 2012 (gmt 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];
</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




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

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

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved