HTML / Javascript: On Click Enable/UnDisable Input Fields?

1.1k views Asked by At

I have the following HTML input boxes:

<form>
<input type="text" name="1" id="1" disabled>
<input type="text" name="2" id="2" disabled>
<input type="text" name="3" id="3" disabled>
<input type="text" name="4" id="4" disabled>
<input type="submit" name="submit" value="submit>
</form>

Next I am trying to use javascript to undisable all my input boxes upon a user clicking my div.

JavaScript:

<script>
    $('#edit').click(function(){
        $('input[]').prop('disabled', false);
});
</script>

Can someone please show me where I am going wrong, thanks

5

There are 5 answers

0
josedefreitasc On

The error is the selector, try:

 $('input').prop('disabled', false);
1
MaThar Beevi On

Try this:

<script src="http://code.jquery.com/jquery-1.8.2.min.js" type="text/javascript"></script>
<script>
$(document).ready(function(){
    $('#edit').click(function(){ 
        $("input").prop('disabled', false);
});
});
</script>
<form>
<input type="text" name="1" id="1" disabled>
<input type="text" name="2" id="2" disabled>
<input type="text" name="3" id="3" disabled>
<input type="text" name="4" id="4" disabled>
<input type="submit" name="submit" value="submit">
</form>
<div id="edit">Click to Enable</div>

whenever using jquery coding before you must write $(document).ready(function(){})

0
R4nc1d On

Check the fiddle

 $('#edit').click(function(){
        $('input').prop('disabled', false);
 });

You can also use the removeAttr and the attr to add and remove the disabled

$('input').removeAttr("disabled");
$('input').attr("disabled", true);
0
Junaid Ahmed On

jQuery 1.6+

To change the disabled property you should use the .prop() function.

$("input").prop('disabled', true);
$("input").prop('disabled', false);

jQuery 1.5 and below

The .prop() function doesn't exist, but .attr() does similar:

Set the disabled attribute.

$("input").attr('disabled','disabled');

To enable again, the proper method is to use .removeAttr()

$("input").removeAttr('disabled');
1
AudioBubble On

Try this:

 $('input:text').prop('disabled', false);

Or

 $('input[type=text]').prop('disabled', false);