简体   繁体   中英

html onclick javascript function call to show something

I want to call gettotal function to get value from textbox and * 3 and show the result in other textbox , code does not work where is the problem!?

<html>
<head>

<script type="text/javascript" language="javascript">
function gettotal()
{
    var x=0;
    x=document.getElementById("pop").value*3;
      document.getElementById("pop1").innerHTML="<b>"+x+"</b>";

}

</script>
<title>hello</title>
</head>
<body>
<form name="form1">
<input type="button" id="btn" name="btn" value="get" OnClick="gettotal()"/>
<input type="text" id="pop" name="pop"  size="3"/>
<br/>
<br/>
<input type="text" id="pop1" name="pop1" size="5" value="val"/>
</form>
</body>

</html>
<html>
<head>

<script type="text/javascript" language="javascript">
function gettotal()
{
    var x=0;
    x=document.getElementById("pop").value*3;
      document.getElementById("pop1").value=x;

}

</script>
<title>hello</title>
</head>
<body>
<form name="form1">
<input type="button" id="btn" name="btn" value="get" OnClick="gettotal()"/>
<input type="text" id="pop" name="pop"  size="3"/>
<br/>
<br/>
<input type="text" id="pop1" name="pop1" size="5" value="val"/>
</form>
</body>

</html>

Instead of .innerHTML() (used to asign HTML tags to element) use .value to assign value to an input field like @Azzi mentioned in his answer :

 function gettotal() { var x=0; x=document.getElementById("pop").value*3; document.getElementById("pop1").value=x; }
 <form name="form1"> <input type="button" id="btn" name="btn" value="get" onClick="gettotal()"/> <input type="text" id="pop" name="pop" size="3"/> <input type="text" id="pop1" name="pop1" size="5" value="val"/> </form>

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM