user580523
user580523

Reputation:

Running code when the page is loaded

I have this link that fills an input when clicked.

<a href="#" onclick="javascript:document.getElementById('input').value='<?php echo("$ms"); ?>'">

I was wondering how I would get it to do this automatically onload.

Upvotes: 0

Views: 52

Answers (4)

Veger
Veger

Reputation: 37906

Add the onLoad event to the <body> element of your document:

<body onload="javascript:document.getElementById('input').value='<?php echo("$ms"); ?>'">

Furthermore, it looks like you are using PHP, if you just want to use $ms to set the value of #input you could also use something like:

<?php
echo '<input type="text" id="input" value="'.$ms.'"/>
?>

Upvotes: 0

markoo
markoo

Reputation: 738

I usually put it in onload, don't know if there is a best practise:

<body onload="myFunc()">

Upvotes: 1

slaphappy
slaphappy

Reputation: 6999

You can use the onload property of an element :

<body onload="javascript:document.getElementById('input').value='<?php echo("$ms"); ?>'">

Upvotes: 0

Quentin
Quentin

Reputation: 943193

Assuming $ms has been made safe for XSS:

<input value="<?php echo $ms; ?>">

Upvotes: 0

Related Questions