Programming Examples

JavaScript program to change the image on button click


Change the Image source using javascript

Insert a image in HTML page and change the image(source) on Button Click.

<!doctype html>
<html>
	<head>
		<script>
			function change_pic()
			{
				document.getElementById("pic").src="b.jpg";
			}
		</script>
	</head>
	<body>
		<img id="pic" src="a.jpg" width="300px"/>
		<br/>
		<input type="button" value="Change Image" onclick="change_pic()"/>
	</body>
</html>
Output/ Explanation:

Latest Current Affairs 2025 Online Exam Quiz for One day Exam Online Typing Test CCC Online Test Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) Online MBA 2 years Online MCA Online BCA Best Website and Software Company in Allahabad