Currently when text area is focused on it expands it’s height and the post and cancel button appear. Now I’d like to disable the submit button by default and only make it active once the text area has been typed into.
Later on I’ll add opacity to the inactive submit button then take it away when the button is active just for a nice effect. But anyway I’ve tried applying disabled many ways and it doesn’t work. I’ve tried various other things such as define a click function as well as submit then apply disabled using attr() to the disabled attribute of my form and it seems to have no effect.
HTML
<div class="comment_container">
<%= link_to image_tag(default_photo_for_commenter(comment), :class => "commenter_photo"), commenter(comment.user_id).username %>
<div class="commenter_content"> <div class="userNameFontStyle"><%= link_to commenter(comment.user_id).username.capitalize, commenter(comment.user_id).username %> - <%= simple_format h(comment.content) %> </div>
</div><div class="comment_post_time"> <%= time_ago_in_words(comment.created_at) %> ago. </div>
</div>
<% end %>
<% end %>
<% if logged_in? %>
<%= form_for @comment, :remote => true do |f| %>
<%= f.hidden_field :user_id, :value => current_user.id %>
<%= f.hidden_field :micropost_id, :value => m.id %>
<%= f.text_area :content, :placeholder => 'Post a comment...', :class => "comment_box", :rows => 0, :columns => 0 %>
<div class="commentButtons">
<%= f.submit 'Post it', :class => "commentButton" %>
<div class="cancelButton"> Cancel </div>
</div>
<% end %>
<% end %>
JQuery:
$(".microposts").on("focus", ".comment_box", function() {
this.rows = 7;
var $commentBox = $(this),
$form = $(this).parent(),
$cancelButton = $form.children(".commentButtons").children(".cancelButton");
// $commentButton = $form.children(".commentButtons").children(".commentButton");
$(this).removeClass("comment_box").addClass("comment_box_focused").autoResize();
$form.children(".commentButtons").addClass("displayButtons");
$cancelButton.click(function() {
$commentBox.removeClass("comment_box_focused").addClass("comment_box");
$form.children(".commentButtons").removeClass("displayButtons");
$commentBox.val("");
});
});
kind regards
Setting the disabled attribute doesn’t work because if the disabled attribute is present, the field is disabled, regardless of the value of the attribute. You could use
.removeAttr("disabled"), but it really makes the most sense to manipulate thedisabledproperty which is more intuitive – it’s just a boolean.To be safe, bind to
onkeyup,oninput, andonchange:If you need to manually enable or disable the button, such as after clearing the form, you can pass
trueorfalsedirectly to a call to.prop().Edit: Detailed breakdown
The
.prop()methodThe
.prop()method gets and sets property values on elements, similar to how.attr()gets and sets attribute values on elements.Considering this jQuery:
We can re-write that in plain JavaScript like this:
Why we don’t need an
ifstatementThe reason we don’t need an
ifstatement is that thedisabledproperty takes a boolean value. We could put a boolean expression in anifstatement, and then depending on the result, assign a different boolean expression as the value of the property:The first problem is the redundancy in the
ifstatement:if (someValue == true)can be shortened toif (someValue). Second, the whole block can be shortened to a single statement by using the logical not operator (!):The logical not operator (
!) returns the negated result of an expression – ietruefor “falsey” values, andfalsefor “truthy” values. If you aren’t familier with “truthy” and “falsey” values, here’s a quick primer. Any time you use a non-boolean value where a boolean is expected, the value is coerced to boolean. Values that evaluate totrueare said to be “truthy” and values that evaluate tofalseare said to be “falsey”.Since an empty string evaluates to false, we can get a boolean indicating whether there is any text in the textarea by applying
!to the value:Or, use
!!to do a double negation and effectively coerce a string value to boolean:We could rewrite the jQuery from above to a more verbose form in plain JavaScript:
Or, a little shorter, and more similar to the original: