14

I have this simple jQuery code to test out.

<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
    $(document).ready(function(){
      $("button").click(function(){
          $("text").attr("disabled","");
      });
    });
</script>
</head>
<body>
<input type="text">
<br />
<button>Set the textfield disabled</button>
</body>
</html>

Basically the HTML page comes with a simple button and textfield. All I want to have the input field disabled as I click the button. But it doesn't work???

(PS: this code is sourced out from w3schools.com website, just to simply test out how powerful jQuery is)

6
  • That explains why W3School isn't appreciated over here. the HTML isn't valid. look at that text element... Commented Jan 27, 2012 at 7:36
  • @gdoron: What's wrong with the <input type="text"> element? It isn't closed properly? Isn't that valid in HTML5? <input> elements never have content, so it's unambiguous. Commented Jan 27, 2012 at 8:16
  • @Mark. Look at my answer. It should be with self closing <input /> like every XML element. Commented Jan 27, 2012 at 9:00
  • 1
    @gdoron: Your answer doesn't explain anything. HTML is not XML (not to be confused with XHTML). It's optional. Read the spec. Or see stackoverflow.com/questions/3008593/… Commented Jan 28, 2012 at 2:25
  • @Mark. You're right that but "Smaller devices often lack the resources or power to interpret a "bad" markup language." so it's bad pracitce writing invalid XHTML code. IMO. Commented Jan 28, 2012 at 16:46

6 Answers 6

18

From jQuery 1.7, you could use .prop:

$(document).ready(function(){
  $("button").click(function(){
      $(":text").prop("disabled", true);
  });
});

Before 1.7, you could do:

$(document).ready(function(){
  $("button").click(function(){
      $(":text").attr("disabled", true);
  });
});

PS: Use $(":text") or $('input[type="text"]') to select all elements of type text.

Sign up to request clarification or add additional context in comments.

Comments

2

Try this:

    <html>   
<head>   
<script type="text/javascript" src="jquery.js"></script>   
<script type="text/javascript">   
$(document).ready(function(){     
$("button").click(function(){         
$("#text").attr("disabled","true");     
});   
});   
</script>   
</head>   
<body>  
 <input id="text" type="text">
<br />
<button>Set the textfield disabled</button>
</body>
</html>

3 Comments

@gdoron Tapas also changed the HTML below
@devnull69. But hided it with a poor indent... =)
Thanks all. I tested the code above and it works! Just I'm still confused about JQuery since choices for formatting your selector can be varied depending on which JQuery version you start off using... Oh well. You learn something new every day.
1

Or (more modern):

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

Comments

1

There is not text selector in jquery. You need to use the attribute selector [attribute=value]

$('input[type=text]').prop('disabled', true); // prop Works on jquery 1.7+

or:

$('input[type=text]').attr('disabled', 'disabled'); // Works in each version. 
                                                    // But isn't W3C standard.

there is a :text selector but it's less efficent then the first option, see the docs:

$(':text') is equivalent to $('[type=text]') and thus selects all elements. As with other pseudo-class selectors (those that begin with a ":") it is recommended to precede it with a tag name or some other selector; otherwise, the universal selector ("") is implied. In other words, the bare $(':text') is equivalent to $(':text'), so $('input:text') should be used instead.

Additional Notes: Because :text is a jQuery extension and not part of the CSS specification, queries using :text cannot take advantage of the performance boost provided by the native DOM querySelectorAll() method. For better performance in modern browsers, use [type="text"] instead.

Note that your's XHTML isn't valid. You should close the <input type="text"> => <input type="text" />

Comments

0

$("input").attr("disabled","disabled");

Comments

-1

"disabled" is a property, not an attribute per-se. Booleans like "checked" or "disabled" don't always get updated (or retrieved) properly when accessing them that way. Use

$(':text').prop('disabled',true);

instead.

2 Comments

$('text') is not a valid selector!
@devnull69: My bad. Thought there was something off about it. :text is though. Updated. I guess I figured he'd have checked if the selector was matching any elements.

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.