...

View Full Version : onclick on JS



gigo1985
05-17-2012, 01:46 PM
hi everybody..


i have this simple code:



<html>
<head>
<script language="javascript" type="text/javascript">
document.write("<input type='button' value='push' onclick='hi()'/>");
function hi() {
alert("hi");
document.write("<input type='button' value='enter' onclick='javascript: bye()'/>");




}
function bye() {
alert("bye");


}
</script>
</head>


<body>




</body>
</html>



if i try to run it on chrom its works well..

but on IE or FireFox its not define the bye() function..

whats the reason?! and how i can solve it?..

thanks very much..:confused::confused::confused:

gigo1985
05-17-2012, 01:49 PM
if i define
function bye(); before defining fuction hi();

its not working eather..

DaveyErwin
05-17-2012, 01:58 PM
<html>
<head>
<script language="javascript" type="text/javascript">

function hi() {
alert("hi");
}
function bye() {
alert("bye");
}
</script>
</head>

<body>
<script language="javascript" type="text/javascript">
document.write("<input type='button' value='push' onclick='hi()'/>");
document.write("<input type='button' value='enter' onclick='javascript: bye()'/>");
</script>

</body>
</html>

xelawho
05-17-2012, 01:58 PM
document.write shouldn't be used for creating html elements or anything so simple - it has its place in advanced javascript, but can and should be avoided for simple operations like this. Much simpler is to hardcode the button and change its attributes when the function is called:


<html>
<head>
<script type="text/javascript">

function hi(btn) {
alert("hi");
btn.value="enter";
btn.onclick=bye;
}
function bye() {
alert("bye");

}
</script>
</head>

<body>
<input type='button' value='push' onclick='hi(this)'/>

</body>
</html>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum