MSK Institute LogoMSK.Institute
HomeCourses
Book Free Demo➔Sign In
Free Demo➔
MSK Institute LogoMSK.Institute

Leading computer training academy in Shikohabad for software engineering, Python, web development, and digital literacy.

ISO 9001:2015 Certified Center
Follow & Connect
4.9

Programs & Admissions

  • All CoursesCatalog
  • Learning PathsRoadmaps
  • Student ProjectsShowcase
  • Live Classes & ScheduleLive
  • Upcoming Batches
  • Campuses & LocationsHub
  • Verify Certificates
  • Book Free Demo Class

Resources & Tools

  • Free Online Mock TestsAll Tests
  • NIELIT CCC Mock TestFree
  • Course & Career FinderQuiz
  • Study Material Hub
  • All MSK ToolsHub
  • Keyboard ShortcutsNew
  • TypeQuest Speed LabTyping
  • MSK Code PlaygroundCode
  • Interactive Tutorials
  • Quick Cheatsheets

Institute & Contact

  • About MSK Institute
  • Careers & InternshipsHiring
  • Publications & Blogs
  • Gali No. 3, Near Gyan Jyoti Public School,
    Adarsh Nagar, Shikohabad, Uttar Pradesh 283135
  • +91 83930 42166
  • mskshikohabad@gmail.com
  • Hours:Mon – Sat: 8:00 AM – 7:00 PM | Sun: 10:00 AM – 2:00 PM
  • 4.9 ★(38 Google Reviews)
© 2026 MSK Institute. All rights reserved.•Privacy Policy•Terms of Service•Disclaimer•Contact Us

Shikohabad's Leading Computer & Coding Academy

Home
Courses
Live
Material
Changing Content and Style0%
Study Materials
JavaScript for BeginnersProgress 0%
1.1What is JavaScript?1.2How JavaScript Works in Browser1.3Setting up Environment: VSCode, Live Server
10.1Selecting Elements: getElementById, querySelector10.2Changing Content and Style10.3Handling Events: click, input, submit
HomeTutorialsJavaScript for BeginnersChanging Content and Style
Handling Events: click, input, submit

Changing Content and Style

Beginner12 min read•Updated: 2026-09-15•By Er. Sumit Kumar (MSK Institute)
Study Materials

Once an element is selected from the DOM, JavaScript can dynamically alter its text content, inject HTML markup, manipulate CSS styles, and add/remove CSS classes to create engaging user experiences.


1. Changing Text & HTML Content

JavaScript provides three distinct properties for updating content:

PropertyBehaviorSecurity Risk
textContentSets or returns the plain text; ignores HTML tags100% Safe (No XSS)
innerTextSimilar to textContent, but respects CSS visibilitySafe
innerHTMLParses string as HTML, inserting tags into the DOMXSS Risk! (Caution)
1const heading = document.querySelector("#title");
2const container = document.querySelector(".card");
3 
4// 1. Updating text safely (Recommended for text):
5heading.textContent = "Welcome to MSK Coding Bootcamp!";
6 
7// 2. Injecting HTML structure:
8container.innerHTML = `
9 <div class="badge">New</div>
10 <p>Course enrollment is now open!</p>
11`;
Important Warning
[!CAUTION] Cross-Site Scripting (XSS) Warning: Never assign unsanitized user input directly to innerHTML! Malicious users can inject <script> tags or inline SVG exploit handlers. Always use textContent when displaying user input.

2. Manipulating Inline CSS Styles (element.style)

You can modify CSS styles directly using the .style property. CSS properties with hyphens (background-color, font-size) are written in camelCase in JavaScript:

1const banner = document.querySelector("#banner");
2 
3banner.style.backgroundColor = "#1e293b";
4banner.style.color = "#ffffff";
5banner.style.fontSize = "24px";
6banner.style.padding = "16px 24px";
7banner.style.borderRadius = "12px";

3. The Superior Approach: Manipulating CSS Classes with classList

Changing individual inline styles with .style leads to messy JavaScript. The professional approach is to define styled classes in CSS and toggle them using classList:

1const alertBox = document.querySelector(".alert");
2 
3// 1. Add one or more classes:
4alertBox.classList.add("alert-success", "fade-in");
5 
6// 2. Remove a class:
7alertBox.classList.remove("alert-warning");
8 
9// 3. Toggle a class (Adds if absent, removes if present!):
10// Perfect for Dark Mode toggles and Hamburger menus!
11alertBox.classList.toggle("dark-mode");
12 
13// 4. Check if element contains a class (Returns true/false):
14if (alertBox.classList.contains("dark-mode")) {
15 console.log("Dark theme is active!");
16}

4. Manipulating Attributes: setAttribute and getAttribute

1const userAvatar = document.querySelector("#avatar");
2 
3// Get an attribute value:
4console.log(userAvatar.getAttribute("src")); // "images/default.png"
5 
6// Set or change an attribute value:
7userAvatar.setAttribute("src", "images/sumit_profile.jpg");
8userAvatar.setAttribute("alt", "Profile photo of Sumit");
9 
10// Or use direct property shortcuts:
11userAvatar.src = "images/sumit_profile.jpg";

Interactive Quiz• Knowledge Check

Practice Quiz

Question 1
Which property should you use to update plain text content safely without risking Cross-Site Scripting (XSS) vulnerabilities?
Question 2
How is the CSS property background-color written when setting styles via JavaScript (e.g. el.style....)?
Question 3
What does element.classList.toggle("active") do?
Question 4
Why is adding/removing CSS classes via classList preferred over writing individual styles with element.style?
Question 5
What method checks whether an element currently has a specific CSS class applied?
Next Lesson

Handling Events: click, input, submit

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Previous LessonNext Lesson
Selecting Elements: getElementById, querySelectorHandling Events: click, input, submit

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

Mentor-Led Practical Training

Master JavaScript for Beginners with Hands-On Projects

Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.

View Full CourseBook Free Demo
PreviousNext
PreviousPrevNext