We help IT Professionals succeed at work.

Bootstrap Form

Howard Bash
Howard Bash asked
on
233 Views
Last Modified: 2017-03-30
Using javascript and/or jQuery,  I need to be able to make a set of form controls read only based on some external (user) criteria.  What is the recommended method to do so?  The form has (buttons, textboxes, date pickers and dropdowns.
Comment
Watch Question

Chinmay PatelChief Technology Ninja
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
Hi Howard,

There are couple of ways, mostly it will rely on how you are going to set criteria. Once you have the list of controls, you could either individually run

$("#controlid").prop('disabled', true);

Open in new window


Or if they are based on a particular class

("input.classname").attr("disabled","disabled");

Open in new window


Regards,
Chinmay.
Howard BashSenior Software Engineer

Author

Commented:
What about if they are all within a form group and I either add a unique id or add a class to the form group class attribute(s)?  It would be nice if I could iterate the controls within and set them all with some loop mechanism.  Is this possible also?
Chief Technology Ninja
CERTIFIED EXPERT
Distinguished Expert 2019
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Howard BashSenior Software Engineer

Author

Commented:
Your example disables all inputs within the div with class 'divId' , I think.  So I would need a similar call for each type of control then, correct?

Inputs, Buttons, Dropdowns, etc...
Chinmay PatelChief Technology Ninja
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
Hi Howard,

No the code will disable all the input controls inside a div with id "divid". It is an ID selector

Please refer to https://api.jquery.com/id-selector/

Regards,
Chinmay.
Howard BashSenior Software Engineer

Author

Commented:
I tried this jQuery statement.  It works but all the controls are grayed out which is sort of ok, but I expect to hear that they don't want the graying to occur.  Is there any way to avoid that?

     $('.form-group .form-control').attr('disabled', 'true');
Chinmay PatelChief Technology Ninja
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
Hi Howard,

Yes. You can do it but I strongly suggest against that. It will frustrate end users if they don't realize that the controls are disabled.

Regards,
Chinmay.
Howard BashSenior Software Engineer

Author

Commented:
I agree, but still, I would like to know how to do that in case (probably when) the customer says,  they don't like the way the form looks when the controls are grayed out...
Chinmay PatelChief Technology Ninja
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
Hi Howard,

You can simply add a stylesheet (same as you disabled the controls) to override default disabled control look.

Regards,
Chinmay.
Howard BashSenior Software Engineer

Author

Commented:
Do you know specifically what styling I need to apply?
Chinmay PatelChief Technology Ninja
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
Hi Howard,

No. It is something you will have to decide. Based on the requirements and customer feedback you can decided how would you want to setup disabled look.

Regards,
Chinmay.
Howard BashSenior Software Engineer

Author

Commented:
Really what I want is for the controls to be readonly, but not have their default style(s) changed beyond that.
Chinmay PatelChief Technology Ninja
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
Hi Howard,

:) Communication... makes life a LOT easier. If you want to make your control just readonly then there is a way.
Try this code
$YourForm.find('#divId input').prop("readonly", true);

Open in new window


Regards,
Chinmay.
Howard BashSenior Software Engineer

Author

Commented:
Hi Chinmay,

The title of the question is how to make a set of controls on a form read only.  How might I have made that more clear.  Your solution says:
"you want to make your control just readonly then there is a way. " 

Where exactly was my lack of clarity?
Chinmay PatelChief Technology Ninja
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
Hi Howard,

You are right. your description talked about read only. I missed that. My bad.

PS: title still says: Bootstrap form.

Regards,
Chinmay.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.