PDA

View Full Version : Run function onclick


ohenecoker
07-21-2008, 06:25 PM
I am trying to run a function when the "onclick" event handler is triggered however it seems not to be working for me. Can anyone help? Here's the code;

<html>
<head>
<style type="text/css">

body {
font:76% normal verdana,arial,tahoma;
}

h1, h2 {
font-size:1em;
}

#foo {
/* simple box */
position:absolute;
left:0px;
top:8em;
width:5em;
line-height:3em;
background:#99ccff;
border:1px solid #003366;
white-space:nowrap;
padding:0.5em;
}

</style>



<script type="text/javascript">
var foo = null;
function doMove(){
foo.style.left = parseInt(foo.style.left)+1+'px';
setTimeout(doMove,20);
}

function init(){
foo = document.getElementById('foo');
foo.style.left ='0px';
doMove();
}

window.onload = init;
</script>
</head>

<body>

<form>
<INPUT ID="counter1" STYLE="position:relative; top:0px" TYPE="button"
VALUE="Move Button"
onclick="document.getElementById('counter1').style.top = '15px';">
</form>

<form>
<input type="button" onclick="doMove();" value="Call function">
</form>

<div id="foo">
</div>
</body>
</html>

Philip M
07-21-2008, 06:37 PM
The script is being triggered onlaod, not by the button.

Delete:- window.onload = init;

Change:-
<form>
<input type="button" onclick="init();" value="Call function">
</form>

ohenecoker
07-21-2008, 06:56 PM
thanxx alot it work :)