John Smith
John Smith

Reputation: 91

JQuery help, How to Hide all button in JQuery

I'm trying to make a request/reply section in my project.
I want to achieve these functionality in that code (that I'm not able to implement; so guys please help me out):
1> When user click on reply button; other reply area(text-area +button) should be hide (means at a time only one reply area should be visible to the user).
2> when user click on reply button text-area will focus and page will slide down (suppose user reply 10 comment focus will automatically set to the 10 number text area and page will slide down to that position accordingly).

Here is my so far code guys:

    //method call on the click of reply link.  
    function linkReply_Clicked(issueId) {    

        Id = issueId;
        textId = "text_" + issueId + count;
        btnReply = "btnReply_" + issueId + count;
        btnCancel = "btnCancel_" + issueId + count;

        var textareasArray = document.getElementsByTagName("textarea");
        var btnArray = document.getElementsByTagName("input");

        for (i = 0; i < textareasArray.length; i++) {
            textareasArray[i].style.display = "none";
            btnArray[i].style.display = "none";
        }

        var str = "<table cellpadding='3' cellspacing='0' width='58%'>";
        str += "<tr><td valign='top' align='left'>";
        str += "<textarea id=" + textId + " rows='5' cols='60'></textarea>";
        str += "</td></tr>";
        str += "<tr><td valign='top' align='right'>";
        str += "<input id=" + btnReply + " type='button' onclick='btnReply_Clicked(Id ,textId)' value='Reply' />&nbsp;";
        str += "<input id=" + btnCancel + " type='button' onclick='btnCancel_Clicked(Id ,textId)' value='Cancel' />&nbsp;";
        str += "</td></tr>";
        str += "</table>";
        document.getElementById("divOuter_" + issueId).innerHTML = str;
        $("#" + textId + "").focus();

    }
    // submit user reply and try to hide that reply area.  
    function btnReply_Clicked(issueId, textID) {

        var comment = document.getElementById(textID).value;
        if (comment != '') {
            $.getJSON("/Issue/SaveComment", { IssueId: issueId, Comment: comment }, null);

            $("#text_" + issueId + count).hide();
            $("#btnReply_" + issueId + count).hide();
            $("#btnCancel_" + issueId + count).hide();
            document.getElementById(textID).value = '';
            count = count + 1;
        }
    }

    // cancel user reply and try to hide that reply area.  
    function btnCancel_Clicked(issueId, textId) {

        $("#text_" + issueId + count).hide();
        $("#btnReply_" + issueId + count).hide();
        $("#btnCancel_" + issueId + count).hide();
        document.getElementById(textId).value = '';
        count = count + 1;
    } 

Upvotes: 0

Views: 696

Answers (1)

Nick Craver
Nick Craver

Reputation: 630379

I changed a bit of this because you can do it much easier since you're already using jQuery :) Go here for the demo version

You can replace all of your posted code with this:

function linkReply_Clicked(issueId) {
    $(".replyTable").hide();        
    var tbl = $("<table class='replyTable' cellpadding='3' cellspacing='0' width='58%'></table>");
    tbl.append("<tr><td valign='top' align='left'><textarea rows='5' cols='60'></textarea></td></tr>");
    tbl.append("<tr><td valign='top' align='right'><input type='button' class='reply' value='Reply' />&nbsp;<input type='button' class='cancel' value='Cancel' />&nbsp;</td></tr>");

    tbl.find(".reply").click(function() {            
        var comment = $(this).closest("table").find("textarea").val();
        if (comment != '') {
            $.getJSON("/Issue/SaveComment", { IssueId: issueId, Comment: comment }, null);
            $(this).closest("div").empty();
        }
    }).siblings(".cancel").click(function() {
        $(this).closest("div").empty();
    });

    var div = $("#divOuter_" + issueId).empty().append(tbl);
    $('html, body').animate({ scrollTop: $(div).offset().top }, 500,
                            function() { div.find("textarea").focus(); });
}

This does the following things differently with the slide effect & the hiding and scrolling from the question:

  • Click handlers for reply/cancel buttons are now in-line, no need for extra functions
  • The inputs no longer have IDs you need to track, it just finds them relatively
  • Table has a class replyTable so it all old ones can be hidden quickly
  • Inputs have classes to find them easier (to bind the click handlers)
  • No more count, no need :)
  • Animates the body, does a quick scroll effect to the location and focuses the text area
  • Removes old tables to cleanup

Upvotes: 2

Related Questions