tags:

views:

41

answers:

3

Hello,

I am wondering if there is a way to NOT show the disabled elements? And if they we're re-enabled (by javascript/jquery) show them?

A: 

You could do the following in jQuery, assuming you use the proper HTML attibrute for your input tags:

$('input[disabled="disabled"']).hide();

Then just add a show() to your jQuery code that re-enables them.

UPDATE

I failed to reference the question title. The above is a solution for form input tags, not select option tags.

Jason McCreary
This won't work cross-browser :)
Nick Craver
will this work if I use a class selector? They have different classes.
Steven
@Nick, you sure? If this were CSS you'd be right. I'll have to check. @Steven, yes, simply add your class to the input: `input.yourclass[disabled="disabled"]`
Jason McCreary
He wants to hide `<option>` elements, not `<input>` you can't hide an `<option>` cross-browser :)
Nick Craver
@Nick, ahhh. I tend to miss those things when it is only in the question title ;)
Jason McCreary
A: 

In CSS you can do the following.

option[disabled]
{
    display:none;
}

Which should hide options that are disabled, not sure about browser compatibility.

Chris Diver
Same as Jason's this won't work cross-browser, need a different approach for what the OP's after.
Nick Craver
Well, it depends on what "cross-browser" means. It works IE7+, Firefox 3+, Safari 3+, Chrome, and Opera 9+ - http://kimblim.dk/css-tests/selectors/
Zurahn
The site needs to work in newish firefox ie 7+ and newish other ones.
Steven
Yep, that's why I said I wasn't sure about browser compatibility.
Chris Diver
@Zurahn - That isn't true, it's not the `[disabled]` I was referring to, it's the `display: none;` Try it yourself: http://jsfiddle.net/BUNUL/
Nick Craver
+1  A: 

Wrap it with a span that hides the element. Not pretty, but it works. E.g.

jQuery

$(document).ready(function() {
  $("#togglebutton").click(function(){
    var Element=$("#disableme");

    if (Element.attr("disabled")==false) {
      Element.attr("disabled","disabled").wrap("<span class='hide'></span>");
    } else {
      Element.removeAttr("disabled");
      Element.parent('span.hide').replaceWith(Element.parent('span.hide').html());
    }
  });
});

CSS

.hide {
  display: none;
}

HTML

<input type="text" id="disableme"><br />
<input type="button" id="togglebutton" value="Toggle disable">

Tested on IE 6, IE 8, Firefox 3.6.6, Opera 10.60 and Iron 5.0.380.

Gert G