tags:

views:

570

answers:

4

i have this function where basically add and remove the disabled attribute form a input field:

$(bla).click(function(){        
  if (something) {
     console.log($target.prev("input")) // gives out the right object
     $target.toggleClass("open").prev("input").attr("disabled", "disabled");
  }else{
     $target.toggleClass("open").prev("input").removeAttr("disabled"); //this works
  }
})

the removeAttr works fine but when i need to add the disabled again it does just nothing. My console.log is triggering (and giving me back the right input field) so I'm sure my that my if statement works. But if i inspect the DOM with firebug the disabled attribute does not appear.

can someone help me?

PS: please don't focus on the function or the if statement itself, works fine its just that attr that does not work for disabled...

edit: its an input type="hidden" is it possible that disabled does not work on hidden fields?

+1  A: 

Try this updated code :

$(bla).click(function(){        
  if (something) {
     console.log($target.prev("input")) // gives out the right object
     $target.toggleClass("open").prev("input").attr("disabled", "true");
  }else{
     $target.toggleClass("open").prev("input").removeAttr("disabled"); //this works
  }
})
Pranay Rana
still doesn't work
meo
this is not the proper way of use toggleClass!
aSeptik
i have just added .attr("disabled", "true"); to posted function
Pranay Rana
+2  A: 

UPDATED

DEMO: http://jsbin.com/uneti3/3

your code is wrong, it should be something like this:

$(bla).click(function() {
    $target.toggleClass('open');   
     var disable = ( $target.hasClass('open') ) ? true: false;
     $target.prev("input").attr("disabled", disable);
  });

you are using the toggleClass function in wrong way

aSeptik
Why do i use it wrong? i just just specify a class and it toggle this one class. This par works fine. (Just like the jquery doc tells me). But still i have tried to do it this way and it does not work to.
meo
the jquery doc say the opposite of what you have done! the doc say that you can use one line of code fo make the same if statment you have! see my updates!
aSeptik
yeah sure its not proper put its not wrong i could just use addclass and remove class but i was lazy ;P but this was not the problem. The problem is that firebug does not change the disabled status in the DOM even if it does. My code works, i just did not see id because my input types where hidden. Thanks for the optimized code, i have adapted it. +1
meo
A: 

Try

$(bla).click(function(){        
  if (something) {
     console.log("A:"+$target.prev("input")) // gives out the right object
     $target.toggleClass("open").prev("input").attr("disabled", "disabled");
  }else{
     console.log("A:"+$target.prev("input")) // any thing from there for a single click?
     $target.toggleClass("open").prev("input").removeAttr("disabled"); //this works
  }
});
Dennis Cheung
+4  A: 

Thank you all for your contribution! I found the problem:

ITS A FIREBUG BUG !!!

My code works. I have asked the PHP Dev to change the input types hidden in to input type text. The disabled feature works. But the firebug console does not update this status!

Here is the proof: firebug bug!!!!!

you can test out this firebug bug by your self here http://jsbin.com/uneti3/3#. Thx to aSeptik for the example page.

meo
thats not actually desiable, its just the param with no value, witch means it stays defualt. you should do disabled="1" to actually disable it :/
RobertPitt
no you should use disabled="disabled" to be w3c. And this is exactly what my code does if you read my initial post
meo
The problem you were having wasn't from Firebug not updating the status, it's with your code. aSeptik's answer is right. If you don't believe me, run your code in Chrome or IE.
fudgey
so test it. Go to aSeptiks example open firebug and change click on the button. As you can see firebug does not update the disabled status. But as you can see visually the status change happens.
meo