2

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

2
  • there is actually no div nor #edit element in your sample code... Commented Jun 18, 2015 at 12:25
  • you are using jquery in your sample code Commented Jun 18, 2015 at 12:32

5 Answers 5

5

The error is the selector, try:

 $('input').prop('disabled', false);
Sign up to request clarification or add additional context in comments.

Comments

3

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(){})

1 Comment

be carefull that from where I see it attaching click event on elements that aren't supposed to be clicked might present a usability issue for people using screen-readers...
1

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);

Comments

1

Try this:

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

Or

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

1 Comment

Better to use $('input[type=text]'). cfr jQuery doc
0

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');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.