2

Hands up - I can't figure it out what's wrong with it. Is that a bug or a wrong code ?

    $(document).ready(function() {
    $("#rem_but").click(function(){

    var mail_name = $("#mail_rem").val();
    var dataString = 'mail_name='+ mail_name;


    if (mail_name.val() == "") {  $("#rem_but").attr("disabled",true); } 
    else {  $("#rem_but").removeAttr("disabled"); };
}); });

So when there's no input the button returns false correctly - when there's an input in the field - still the button returns false, hence the removeAttr() doesn't work - why ? Regards.

Arnaud Le Blanc
  • 90,979
  • 22
  • 192
  • 188
Mr X
  • 133
  • 1
  • 11

3 Answers3

5

try (mail_name.val() == "") change to (mail_name == "")

simoncereska
  • 2,967
  • 14
  • 23
3

Are you using jQuery 1.6.x?

If so then you should try using the .prop() function. See below:

Disable/enable an input with jQuery?

Also, in your if statement no need to keep selecting $("#rem_but"). Based on your code I would recommend $(this) instead -

$(this).prop('disabled', true);

This should work -

$(document).ready(function() {
 $("#rem_but").click(function(e) {

 e.preventDefault();

 var mail_name = $.trim($("#mail_rem").val());
 var dataString = 'mail_name='+ mail_name;

 if (mail_name === "") {  
    $(this).prop("disabled", true); }
 else {  
    $(this).prop("disabled", false); }
 });
});

Here is the working jsFiddle code -

http://jsfiddle.net/4rPc5/

Updated code -

http://jsfiddle.net/4rPc5/2/

Community
  • 1
  • 1
Kris Krause
  • 7,134
  • 2
  • 20
  • 26
  • Upgrade? I cannot answer that for you because I do not understand the implications or your application. – Kris Krause Sep 19 '11 at 18:52
  • Is this a new app or a small app? Then I would answer yes. Do you have unit tests to double-check? – Kris Krause Sep 19 '11 at 18:53
  • It's a small poput window with one field for a mail address input ( to reset it ) a very simple code...When the field is empty the button works fine returning false , when not - the button still returns false and removeAtrr() or prop() doesn't work for some reason...( I'm not sure if I used prop() method correctly though - I'm using 1.6.4 version now ) – Mr X Sep 19 '11 at 18:56
  • I just updated the example code to include another button to enable the first button. And it also uses the keyup() event so you do not need an extra button. – Kris Krause Sep 19 '11 at 19:16
  • thanks a lot - I saw the code. works but is this a workaround ? I just need one simple button that submits ( enables ) when there's anything in the field and simpy doesn't when there's nothing....It looks like a bug to me..( I don't want to implement 3 buttons - although I appreciate your solution of course ! ) – Mr X Sep 19 '11 at 19:33
  • "return" out of the function when the value is an empty string to not continue to the ajax call. – Kris Krause Sep 19 '11 at 19:43
  • Thank you Kris ! Indeed - this one is working http://jsfiddle.net/4rPc5/5/ Regards.. – Mr X Sep 19 '11 at 19:54
0

Perhaps you need to set the disabled attribute to 'false'?

if (mail_name.val() == "") {  $("#rem_but").attr("disabled",true); } 
else {  $("#rem_but").attr("disabled",false); };
}

Or set it to an empty string

if (mail_name.val() == "") {  $("#rem_but").attr("disabled",true); } 
else {  $("#rem_but").attr("disabled",""); };
}
jcadcell
  • 793
  • 8
  • 18
  • Hi..I've been trying to do so as well - to no avail...Now I upgraded to 1.6.4...so I don't think you can use attr()..P.S. Your method didn't work in the previous version, I tried it.. – Mr X Sep 19 '11 at 19:12