jQuery Events: Change the background color of a element
jQuery Events : Exercise-8 with Solution
Change the background color of the <div> element of the following code on clicking the button.
Sample Data :
HTML :
<div style="background-color:yellow"> <p>Click the button to change the background color of this paragraph.</p> <button>Click me!</button> </div>
Solution :
HTML Code :
<!DOCTYPE html>
<html>
<head>
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
<div style="background-color:yellow">
<p>Click the button to change the background color of this paragraph.</p>
<button>Click me!</button>
</div>
</body>
</html>
JavaScript Code:
$("div").on( "click", "button", function( event ) {
$(event.delegateTarget ).css( "background-color", "green");
});
Live Demo:
See the Pen jquery-events-exercise-8 by w3resource (@w3resource) on CodePen.
Contribute your code and comments through Disqus.
Previous: Find the data passed with the on() method for each <p> element.
Next: Find the position of the mouse pointer relative to the left and top edges of the document.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework