Making if -statement in jQuery

How can you say the following in jQuery?

If If textarea AND input.title are NOT empty, then put input.button ON.

My attempt in pseudo-code

if ( $(textarea).not.empty() AND $(input.title).not.empty() ) {
   $('.ask_question').attr('enabled, 'enabled'); 
}

Upvotes: 2

Views: 7354

Answers (5)

Fenton
Fenton

Reputation: 251262

if ($("#myTextArea").val().length != 0 && $("#myTextBox").val().length != 0) {
   $('.ask_question').removeAttr("disabled"); 
}

You can use "textarea" and "input[class='title']", but it is possible for these to return multiple results.

Upvotes: 0

Mike
Mike

Reputation: 2587

if ( $("textarea:empty").length == 0 && $("input.title:empty").length == 0 ) {
   $('.ask_question').attr('enabled', 'enabled'); 
}

The method property length of jQuery returns the number of elements which were selected. :empty is a selector for jQuery to select elements which have no child or no text.

So,

if (number of empty textarea is 0) AND (number of empty input with the class title is 0) then
   enable somthing!

Upvotes: 1

Mickel
Mickel

Reputation: 6704

What would jQuery-jesus do?

$('textarea').is(":contains('')")

or

$('input.title').is(":empty")

I suppose ;)

Upvotes: 4

Andy Gaskell
Andy Gaskell

Reputation: 31761

Not too far off.

if ( $("textarea").val().length && $("input.title").val().length ) {
  $('.ask_question').removeAttr('disabled'); 
}

Upvotes: 0

stefita
stefita

Reputation: 1793

if ( $("textarea").val() != "" && $("input.title").val() != "" ) {
   $('.ask_question').attr('enabled', 'enabled'); 
}

Upvotes: -1

Related Questions