-3

I am getting issue with jQuery prop method. If I am setting value dynamically, and then disabling the input then the value is hiding. Below is my code:

$("#firstName").val("First Name");
$("input").prop("disabled",true);

and now clicking edit button and removing disabled.

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

at this point of time, my value is again visible. Can anybody help me in understanding this and make that value visible even on disable mode? Thanks

Alessio Cantarella
  • 4,659
  • 3
  • 21
  • 29
Walberg
  • 27
  • 6
  • 1
    `disabled` doesn't make it invisible it prevent users entering values/submitted in the form – Liam Nov 22 '16 at 09:56
  • i understand that. but i m unable to see the values which i added after disabling to true – Walberg Nov 22 '16 at 09:58
  • 1
    Your question isn't clear. What do you mean *unable to see*? Please create a [mcve](http://stackoverflow.com/help/mcve) – Liam Nov 22 '16 at 09:58
  • Please provide [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve), it's impossible to answer with this information. – Esko Nov 22 '16 at 09:59
  • This does not hide the text in Chrome on windows: `$("#firstName").on("click", function() { $("#firstName").val("First Name"); $("input").prop("disabled", true); })` - it only greys the field out but the text is still visible. Please click the `<>` button and create a snippet that shows the issue – mplungjan Nov 22 '16 at 09:59
  • Please see the pen . [link text](http://codepen.io/walberg/pen/JbWVxK) – Walberg Nov 22 '16 at 10:21

2 Answers2

0

Here is the working JSFiddle which I made for you. The problem is with your jQuery script loading.

<input type="text" value="firstName" disabled="true" />
<button id="chProp">
    Change Prop
</button>

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

Hope it will solve your issue.

marc_s
  • 675,133
  • 158
  • 1,253
  • 1,388
0

For jQuery 1.6+

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

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

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

For more details Reference link.

Community
  • 1
  • 1
Sachin
  • 94
  • 8