Write a variable to a form input value with script

please I need help since I am new in this step

what I need is that document.write (getMensaje ());
is inside the input box

someone could correct me or help me, thank you very much

<html>
<head>
<style>
		body {
			background-color: #f6f6f6;
			color: #555;
			line-height: 1.7;
		}
		center {

		}
		center div {
			padding: 0 10vw;
			/* text-align: justify; */
		}
		* {
			font-family: Raleway, monospace, sans-serif;
			font-weight: 300;
			font-size: 17px;
		}
		a {
			text-decoration: none;
			color: royalblue;
		}
		a:hover {
			color: red;
		}
		b {
			font-size: 20px;
			font-weight: bold;
		}
		input {
			text-align: center;
			outline: none;			
		}
		input.ffpnote {
			font-size: 22px;
			font-weight: bold;
			color: black;
			margin: 20px 0;
			padding: 8px;
			width: 160px;
		}
		input.paypalemail {
			font-weight: bold;
			color: black;
			margin: 0 10px;
			padding: 4px 0;
		}
		img {
			margin-bottom: 50px;
		}
	</style>
<script>
function getMensaje(){
var aFrases=new Array();
aFrases[0]="test0";
aFrases[1]="test01";
aFrases[2]="test02";
aFrases[3]="test03";
return(aFrases[Math.floor(Math.random() * aFrases.length)]);
}
</script>
</head>
<body>
<script>
document.write(getMensaje());
</script>

<input onclick="select()" class="paypalemail" style="width:200px" value="document.write(getMensaje());">
</body>
</html>

Hi @omaritotigo, the value attribute doesn’t get evaluated as JS, and that’s not how document.write() works anyway – see here for details. However you can set the value of an input element like so:

<input type="text" id="my-input">
<script>  
function getMensaje () {
 var phrases = ['foo', 'bar', 'baz']
 return phrases[Math.floor(Math.random() * phrases.length)]
}

// Get a reference to the input element
var myInputElement = document.getElementById('my-input')
// Set the value to the value returned by calling getMensaje()
myInputElement.value = getMensaje()
</script>

could you help me in my code i’m newbie with this topic

would be of great appreciation

Please

What do you mean? The snippet I posted above would work as is – just put it inside the HTML body. I just added an ID to the input element, and omitted the other attributes for clarity that are not relevant to this functionality.

Thank you very much for all your help, I thank you very much
regards

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.