0

I want to set a value to a field, this is the html:

<form name="myForm"> 
<input type="text" id="user">
</form>

And the javascript:

window.onload = init;
function init() {
generateUser();
//more code... 
}

function generateUser(){
var generated = "usr_"+Math.floor((Math.random() * 1000) + 1);
document.myForm.user.value(generated);
}

I cant find a way to set that value, always getting "String not a function".

2
  • 2
    value isn't a function. It's a string. It's the value of the input field. Commented Nov 13, 2014 at 18:55
  • 1
    Don't confuse it with .val() from JQuery Commented Nov 13, 2014 at 19:00

2 Answers 2

6

Should be:

document.myForm.user.value = generated;

It is not a method it is a property...

Sign up to request clarification or add additional context in comments.

Comments

1

Exists very useful function to find element in any place on the page by element id - getElementById, you can use it

document.getElementById("user").value = generated;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.