Write a javascript code to print the table of a number entered by the user using switch statement.
Answers
<script type="text/javascript">
function printTable(){
var num;
num=Number(document.getElementById('txtNumber').value);
for(var i=1; i<=10; i++){
var pTag= document.getElementById('pPrint');
pTag.innerHTML += (num*i) + "<br/>"
}
}
</script>
<script type="text/javascript">
function printTable(){
var num;
num=Number(document.getElementById('txtNumber').value);
for(var i=1; i<=10; i++){
var pTag= document.getElementById('pPrint');
pTag.innerHTML += (num*i) + "<br/>"
}
}
</script>
HTML Source Code with JavaScript
<!--JavaScript - Function to Display Table of an entered Number.-->
<html>
<head>
<title>JavaScript - Function to Display Table of an entered Number.</title>
<script type="text/javascript">
function printTable(){
var num;
num=Number(document.getElementById('txtNumber').value);
for(var i=1; i<=10; i++){
var pTag= document.getElementById('pPrint');
pTag.innerHTML += (num*i) + "<br/>"
}
}
</script>
</head>
<body style="text-align: center;">
<h1>JavaScript - Function to Display Table of an entered Number.</h1>
<p><input type="text" id="txtNumber" placeholder="Enter number"/></p>
<p><input type="button" value="Print Table." onclick='printTable()'/>
<!--print numbers-->
<p id="pPrint"></p>
</body>
</html>
Result
print table of a number, display table of entered number