Write jQuery code to change the text of a heading when the page loads
Description: The page has a heading with the text “Loading…”. When the page is ready, change it to “Welcome to jQuery”.
Hint:
- Use
$(document).ready()and the.text()method.
HTML of the page
<h1 id="title">Loading...</h1>
Solution
$(function () {
$('#title').text('Welcome to jQuery');
});
HTML after the code runs
<h1 id="title">Welcome to jQuery</h1>
How it works
- The ready function waits until the DOM is built, so the heading exists.
.text()with a value replaces the text of the element.
Read Document Ready and Set Content.
Learn more about the similar topics:
| Tutorials |
|---|
| No Content Found. |
| Exercises & Assignments |
|---|
| No Content Found. |
| Interview Questions & Answers |
|---|
| No Content Found. |