Introductions
JavaScript can dynamically change HTML content, CSS styles, classes, and HTML attributes after a webpage loads. These features are essential for creating interactive websites. In this chapter, you will practice changing headings, paragraphs, images, links, classes, styles, and attributes using simple JavaScript examples. JavaScript HTML, CSS and Attributes Practice Questions with Solutions help to understand the concepts.
Question 1: Change HTML Text with JavaScript
Problem
Create a heading and use JavaScript to change its text when the page loads.
Solution
<h1 id="heading">Old Heading</h1>
<script>
const heading = document.getElementById("heading");
heading.textContent = "Welcome to My Website";
</script>
Output
Welcome to My Website
Step-by-step Explanation
- The
<h1>has the IDheading. getElementById()selects the heading.- The selected element is stored in
heading. textContentchanges the existing text.- The browser displays the new heading.
Question 2: Change the HTML Content Using innerHTML
Problem
Create an empty <div> and use JavaScript to add a heading and paragraph inside it.
Solution
<div id="container"></div>
<script>
const container = document.getElementById("container");
container.innerHTML = `
<h2>JavaScript DOM</h2>
<p>JavaScript can change HTML content.</p>
`;
</script>
Output
JavaScript DOM
JavaScript can change HTML content.
Step-by-step Explanation
- The
<div>starts empty. - JavaScript selects the
<div>. innerHTMLallows HTML markup to be inserted.- A heading and paragraph are added.
- The browser renders the new HTML.
Question 3: Change CSS Properties Using JavaScript
Problem
Create a paragraph and use JavaScript to change its color, font size, and background color.
Solution
<p id="message">JavaScript can change CSS.</p>
<script>
const message = document.getElementById("message");
message.style.color = "blue";
message.style.fontSize = "24px";
message.style.backgroundColor = "yellow";
</script>
Output
The paragraph appears with:
- Blue text
- 24px font size
- Yellow background
Step-by-step Explanation
- JavaScript selects the paragraph.
style.colorchanges the text color.style.fontSizechanges the font size.style.backgroundColorchanges the background.- JavaScript uses CSS property names in camelCase.
For example:
background-color
becomes:
backgroundColor
Question 4: Add a CSS Class Using classList.add()
Problem
Create a paragraph and add a CSS class to it using JavaScript.
Solution
<style>
.highlight {
color: white;
background-color: black;
padding: 10px;
}
</style>
<p id="message">Important Message</p>
<script>
const message = document.getElementById("message");
message.classList.add("highlight");
</script>
Output
The paragraph receives the highlight styling.
Step-by-step Explanation
- The
.highlightCSS class defines several styles. - JavaScript selects the paragraph.
classList.add("highlight")adds the class.- The browser immediately applies the CSS rules.
- JavaScript does not need to write every CSS property separately.
Question 5: Remove a CSS Class Using classList.remove()
Problem
Create a paragraph with a CSS class and remove that class using JavaScript.
Solution
<style>
.highlight {
background-color: yellow;
color: red;
}
</style>
<p id="message" class="highlight">Highlighted Text</p>
<script>
const message = document.getElementById("message");
message.classList.remove("highlight");
</script>
Output
The yellow background and red text are removed.
Step-by-step Explanation
- The paragraph initially has the
highlightclass. - JavaScript selects the paragraph.
classList.remove()removes the class.- The styles defined by
.highlightare no longer applied.
Question 6: Toggle a CSS Class
Problem
Create a button and paragraph. When the button is clicked, toggle a CSS class on the paragraph.
Solution
<style>
.active {
color: white;
background-color: green;
padding: 10px;
}
</style>
<p id="message">Click the button to change my style.</p>
<button id="toggleButton">Toggle Style</button>
<script>
const message = document.getElementById("message");
const button = document.getElementById("toggleButton");
button.addEventListener("click", function() {
message.classList.toggle("active");
});
</script>
Output
Before clicking:
Click the button to change my style.
[Toggle Style]
After clicking, the paragraph receives the active class.
Clicking again removes the class.
Step-by-step Explanation
- The paragraph starts without the
activeclass. - The button listens for a click.
classList.toggle("active")checks the class.- If the class does not exist, it adds it.
- If the class already exists, it removes it.
- This makes
toggle()useful for interactive styles.
Question 7: Change an Image Attribute
Problem
Create an image and use JavaScript to change its src and alt attributes.
Solution
<img id="productImage" src="old-image.jpg" alt="Old Product">
<script>
const image = document.getElementById("productImage");
image.setAttribute("src", "new-image.jpg");
image.setAttribute("alt", "New Product");
</script>
Output
The image now uses:
src = new-image.jpg
alt = New Product
Step-by-step Explanation
- JavaScript selects the image.
setAttribute()changes an HTML attribute.- The first call changes
src. - The second call changes
alt. - The browser uses the updated attribute values.
Question 8: Change a Link Using JavaScript
Problem
Create a link and use JavaScript to change its destination and displayed text.
Solution
<a id="websiteLink" href="https://example.com">
Visit Website
</a>
<script>
const link = document.getElementById("websiteLink");
link.href = "https://www.google.com";
link.textContent = "Visit Google";
</script>
Output
Visit Google
The link now points to the new destination.
Step-by-step Explanation
- The
<a>element has an ID. - JavaScript selects the link.
link.hrefchanges the destination.textContentchanges the visible link text.- Both HTML properties are updated dynamically.
Question 9: Change Multiple CSS Properties with a CSS Class
Problem
Instead of changing multiple styles individually, create a CSS class and apply it to a box using JavaScript.
Solution
<style>
.card-style {
background-color: lightblue;
color: black;
padding: 20px;
border: 2px solid black;
border-radius: 10px;
}
</style>
<div id="card">
JavaScript Card
</div>
<script>
const card = document.getElementById("card");
card.classList.add("card-style");
</script>
Output
The <div> appears as a styled card.
Step-by-step Explanation
.card-stylecontains all required CSS.- JavaScript selects the
<div>. classList.add()applies the CSS class.- All styles are applied together.
- This approach keeps JavaScript cleaner when many styles need to change.
Question 10: Change HTML, CSS and Attributes Together
Problem
Create a button and a profile card. When the button is clicked:
- Change the person’s name.
- Change the card’s CSS class.
- Change the image
altattribute.
Solution
<style>
.normal-card {
padding: 15px;
border: 1px solid black;
}
.active-card {
padding: 15px;
border: 3px solid green;
background-color: lightgreen;
}
</style>
<div id="card" class="normal-card">
<h2 id="name">Rahul</h2>
<img
id="profileImage"
src="profile.jpg"
alt="Rahul Profile"
width="150"
>
</div>
<button id="updateButton">Update Profile</button>
<script>
const card = document.getElementById("card");
const name = document.getElementById("name");
const image = document.getElementById("profileImage");
const button = document.getElementById("updateButton");
button.addEventListener("click", function() {
name.textContent = "Aman";
card.classList.remove("normal-card");
card.classList.add("active-card");
image.setAttribute("alt", "Aman Profile");
});
</script>
Output
Before clicking:
Rahul
[Profile Image]
[Update Profile]
After clicking:
Aman
[Updated Profile Image]
[Update Profile]
The card also receives the active-card styling.
Step-by-step Explanation
- JavaScript selects the card.
- JavaScript selects the name heading.
- JavaScript selects the image.
- JavaScript selects the button.
- A
clickevent is attached to the button. textContentchanges"Rahul"to"Aman".classList.remove()removes the old card style.classList.add()applies the new card style.setAttribute()changes the image’saltattribute.- All three parts of the webpage are updated without reloading the page.
Key Takeaways
- JavaScript can dynamically modify HTML content.
textContentchanges text inside an element.innerHTMLcan insert HTML markup.- The
styleproperty can directly modify CSS. classList.add()adds a CSS class.classList.remove()removes a CSS class.classList.toggle()adds or removes a class depending on its current state.setAttribute()changes HTML attributes.getAttribute()can read an attribute.- Properties such as
src,href, andaltcan also be changed directly. - JavaScript can combine HTML, CSS, and attribute changes in one interaction.
- These techniques are the foundation of interactive webpages.
FAQs
1. How can JavaScript change HTML text?
Use textContent.
const heading = document.getElementById("heading");
heading.textContent = "New Heading";
2. What is the difference between textContent and innerHTML?
textContent treats the assigned value as text:
element.textContent = "<strong>Hello</strong>";
innerHTML interprets the value as HTML:
element.innerHTML = "<strong>Hello</strong>";
3. How can JavaScript change CSS?
You can directly change styles:
element.style.color = "red";
element.style.fontSize = "20px";
For multiple styles, using a CSS class is often cleaner.
4. How do I add a CSS class using JavaScript?
Use classList.add().
element.classList.add("active");
5. How do I remove a CSS class?
Use classList.remove().
element.classList.remove("active");
6. How do I toggle a CSS class?
Use classList.toggle().
element.classList.toggle("active");
It adds the class if it is missing and removes it if it already exists.
7. How can JavaScript change an HTML attribute?
Use setAttribute().
element.setAttribute("title", "New Title");
You can also directly modify common properties:
image.src = "new-image.jpg";
link.href = "https://example.com";
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
