Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Script to enable/disable input elements?

Tags:

I'm wondering if it's possible for a script to enable/disable all input elements on the page with some sort of toggle button.

I googled it but didn't find anything too useful except for this:

http://www.codetoad.com/javascript/enable_disable_form_element.asp but I'm not sure how to edit it for the toggle.

like image 524
Charlie Avatar asked Jul 26 '11 19:07

Charlie


People also ask

How do I disable input element?

The disabled attribute can be set to keep a user from using the <input> element until some other condition has been met (like selecting a checkbox, etc.). Then, a JavaScript could remove the disabled value, and make the <input> element usable. Tip: Disabled <input> elements in a form will not be submitted!

How do I make input disabled in CSS?

you can disable via css: pointer-events: none; Doesn't work everywhere though. Text input can still be tabbed into.

How can I prevent a user using inspect element to enable a disabled element?

You can disable F12, right click, ctrl button etc. If the information being edited so sensitive that "it is scary" just validate server side. They can use tools like fiddler to make Http Requests that you don't desire.


2 Answers

Something like this would work:

var inputs=document.getElementsByTagName('input'); for(i=0;i<inputs.length;i++){     inputs[i].disabled=true; }    
like image 148
Adam MacDonald Avatar answered Oct 11 '22 16:10

Adam MacDonald


A working example:

$().ready(function() {          $('#clicker').click(function() {          $('input').each(function() {              if ($(this).attr('disabled')) {                  $(this).removeAttr('disabled');              }              else {                  $(this).attr({                      'disabled': 'disabled'                  });              }          });      });  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>  <input type='text'></input>  <input type='text'></input>  <input type='text'></input>      <div id='clicker' style='background-color:#FF0000; height:40px; width:100px;'></div>
like image 37
samccone Avatar answered Oct 11 '22 16:10

samccone