Learn CSS from the Best Tutors
Search in
You can change the CSS class of a textbox (or any other HTML element) using JavaScript by manipulating the classList
property. Here's an example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
.originalClass {
/* Your original styles here */
border: 1px solid #ccc;
padding: 5px;
}
.newClass {
/* Your new styles here */
border: 2px solid #ff0000; /* Red border as an example */
padding: 10px;
}
</style>
<title>Change CSS Class Example</title>
</head>
<body>
<input type="text" id="myTextbox" class="originalClass">
<script>
// Function to change the class
function changeClass() {
var textbox = document.getElementById("myTextbox");
// Remove the current class
textbox.classList.remove("originalClass");
// Add the new class
textbox.classList.add("newClass");
}
// Call the function to change the class (you can trigger this in response to an event)
changeClass();
</script>
</body>
</html>
In this example, we have an input textbox with an initial class of "originalClass." The JavaScript function changeClass
is defined to remove the original class and add a new class ("newClass"). You can trigger this function in response to an event such as a button click, form submission, or any other user action.
Make sure to replace the styles and class names with your own as needed.
Related Questions
Why is HTML is used in Web Application?
Now ask question in any of the 1000+ Categories, and get Answers from Tutors and Trainers on UrbanPro.com
Ask a QuestionRecommended Articles
Skills Required to Become a Front End Web...
Today, no business can exist without having its own website to interact with its customer base. Having a website is no more restricted to the big MNCs. With the advancement in technology and global business, even the small enterprises are spending on having their own websites and development teams. The person works on web...
Why Should you Learn HTML
Since the world today is ruled by the Internet, everyone desires to build a website for either business or personal interests. HTML or Hyper-text Mark-up Language is a globally standardized language which used to format web pages. By using HTML, the appearance of text, links, images and almost every part of a web page could...
Make a Career in Mobile Application Programming
Almost all of us, inside the pocket, bag or on the table have a mobile phone, out of which 90% of us have a smartphone. The technology is advancing rapidly. When it comes to mobile phones, people today want much more than just making phone calls and playing games on the go. People now want instant access to all their business...
Learn Hadoop and Big Data
Hadoop is a framework which has been developed for organizing and analysing big chunks of data for a business. Suppose you have a file larger than your system’s storage capacity and you can’t store it. Hadoop helps in storing bigger files than what could be stored on one particular server. You can therefore store very,...
Looking for CSS Training?
Learn from the Best Tutors on UrbanPro
Are you a Tutor or Training Institute?
Join UrbanPro Today to find students near youThe best tutors for CSS Classes are on UrbanPro
The best Tutors for CSS Classes are on UrbanPro