Home » Javascript

JavaScript - Function to Display Table of an entered Number

In this example we will learn how to print table of an integer number using JavaScript function. In this example we will take an integer value as input through text box and will display table of that number in a paragraph tag.

Except of table printing we will learn read value an integer through textbox, converting string to integer and printing text into paragraph using .innerHTML.

JavaScript Code Snippet - Function to Print Table of an Entered Number

JavaScript function

<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




Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.