$(document).ready(function() { function updateTalkBox(listing_id) { $.ajax({ type: "GET", url: "/talk_posts/view_by_listing/"+listing_id, success: function(html){ alert( "Data: " + html ); $(".talk_box").find(".content").replaceWith(html); } }); } var CHAR_LIMIT = 140; $(".talk_box").find(".active").hide(); $(".talk_box").find(".active").find("input").attr("disabled", true); //show talkpost options on focus $(".talk_box").find("textarea").focus( function() { $(this).parent().nextAll(".active").slideDown("fast"); }); //scroll to bottom of talkbox //$(".talk_box").find(".content").attr({ scrollTop: $(".talk_box").find(".content").attr("scrollHeight") }); scrollDown(); //hides the talkpost options on blur $(window).click( function(e) { if (!$(e.target).is('input') && !$(e.target).is('textarea') && !$(e.target).is('label')) $(".talk_box").find(".active").slideUp("fast"); //setTimeout("hideForm()", 1000); }); //checks counts while typing $(".talk_box").find("textarea").keyup( function(e) { if(!updateCharCounter()) $(this).parent().nextAll(".active").find("input").attr("disabled", true); else $(this).parent().nextAll(".active").find("input").removeAttr("disabled"); }); //submit form $(".talk_box").find(".submit").find("input").click( function (e) { e.preventDefault(); var form_data = $(".talk_box").find("form").serialize(); $.ajax ({ type: "POST", url: "../../talk_posts/add/true", data: form_data, success: function(html){ $(".talk_box").find(".content").fadeOut('fast', function() { $(".talk_box").find(".content").replaceWith(html).fadeIn('fast', scrollDown()); } ); $(".talk_box").find("textarea").val(""); $(".talk_box").find(".count").text(""); //var objDiv = $(".talk_box").find(".content"); //alert ("hi"); //$(".talk_box").find(".content").animate({ scrollTop: $(".talk_box").find(".content").attr("scrollHeight") }, 3000); scrollDown(); } }); }); //scrolls down talk box function scrollDown() { $(".talk_box").find(".content").animate({ scrollTop: $(".talk_box").find(".content").attr("scrollHeight") }, 500); } function updateCharCounter() { var value = $(".talk_box").find("textarea").val(); var remaining = 140 - value.length; $(".talk_box").find(".count").text(remaining); if ((remaining >= 0) && (remaining < 140)) return true; else return false; } });