Post Top Ad

Tuesday, November 21, 2017

JavaScript DOM Exercise 1 with Answer





Here is a sample html file with a submit button. Now modify the style of the paragraph text through javascript code



<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>JS DOM paragraph style</title>
</head>
<body>
<p id ='text'>JavaScript Exercises - by Mr Khem Puthea</p>
<div>
<button id="jsStyle"
onclick="js_style()">Style</button>
</div>
</body>
</html>

Here is the answer : 
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>JS DOM paragraph style</title>
</head>
<body>
<p id ='text'>JavaScript Exercises - by Mr Khem Puthea</p>
<div>
// use onclick action
<button id="jsStyle" onclick="jsStyle()">
Style
</button>
</div>
<script>
function jsStyle() {
text.style.fontSize = "14pt"; // change font size
text.style.fontFamily = "Comic Sans MS"; // change font
text.style.color = "green"; // change color
}
</script>
</body>
</html>




No comments:

Post a Comment