?
Solved

How can I make a css class read only with jquery?

Posted on 2016-11-30
2
Medium Priority
?
146 Views
Last Modified: 2016-11-30
I have a class on a table row like the below. Can someone tell me how to make it read only with jQuery, so everything in that class will be read-only/not interactive (I have some drop downs etc in the row)?

<table>
     <tr class="colorCars">.
......

Open in new window

0
Comment
Question by:earwig75
2 Comments
 
LVL 54

Expert Comment

by:Scott Fell, EE MVE
ID: 41907539
Add the class as you did, but don't place it in your css file and and you can use css http://api.jquery.com/css/.  

You can also use addClass https://api.jquery.com/addclass/.  

CSS
.colorCars{
  /* nothing */
}

.colorCars.active{
  color:red;
}

Open in new window


JS
$(".colorCars").on("click", function(){
       $(this).addClass( "colorCars active" )
});

Open in new window


Depending on what you want to do and why, there are going to be many options.
0
 
LVL 60

Accepted Solution

by:
Julian Hansen earned 2000 total points
ID: 41907720
This will make readonly all the controls in that row.
If you are only using input and textareas you can set the readonly property like this
$('.colorCars select, .colorCars input, .colorCars textarea').prop({readonly: true});

Open in new window

This won't work on radio, checkbox and select controls - you would need to disable those like this
$('.colorCars select, .colorCars input, .colorCars textarea').prop({disabled: true});

Open in new window


Here is a sample that demonstrates both

Use the Mode selector to toggle between readonly and disabled to see how different controls react
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
In this tutorial viewers will learn how to customize the background color and font color of highlighted text using the ::selection element in CSS Begin by defining the selected text as an element in CSS by typing "::selection": Style the ::selection…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses
Course of the Month16 days, 15 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question