Patriotec
Patriotec

Reputation: 1144

Need JQuery to stop repeating for every button thats clicked

The basics of the functions work, but for every cancel button thats clicked, the next button will add that number of clicks to it. For example if a user clicks a cancel button once, then next time a different button with a different id is clicked, firebug shows the page that contains the form (page2) to load that many times, instead of once. This all generated dynamically, so there may be up to 10 of these div's on the parent page. What should happen is:

  1. user clicks the readonly textbox on the parent page
  2. onfocus empties that div from parent page and loads the child page 3.form from the child page is in the readonly's textbox space now
  3. user either submits a comment or cancels
  4. either action should put the readonly textbox back in the page (up to this point, this all works)
  5. when pressing another div to leave a comment that page should only load once, not as many times from clicking the previous buttons.

In the 1st page I have:

<head>
    <script type="text/javascript">
        $(document).ready(function(){
            var ajaxInProgress = false;
            $(function(){
                if (ajaxInProgress) return;
                $('.ce').focus(function(){
                    var cid = this.id;
                    $('#comfield'+cid).empty();
                    $('#comfield'+cid).load('content_comment.php?id=<%=intmemberid%>&cid=' + cid);
                });
            });
    </script>
</head>
<body>

    <div id="comform"
        <div id="comfield2">
            <input class="ce" id="2" type="text" value="Write a comment..." readonly="readonly" />
        </div>
    </div>
    <div id="comdata2"></div>

    <div id="comform"
        <div id="comfield3">
            <input class="ce" id="3" type="text" value="Write a comment..." readonly="readonly" />
        </div>
    </div>
    <div id="comdata3"></div>

</body>

In page 2 I have

<script type="text/javascript">
    $(document).ready(function() {

        document.commentform2.comment.focus();

        $("#cancelcomment2").click(function(){
            $('#comfield2').empty();
            $('#comfield2').html('<input class="ce" id="2" type="text" value="Write a comment..." readonly="readonly" />');
            var ajaxInProgress = false;
            $(function(){
                $('.ce').focus(function(){
                    if (ajaxInProgress) return;
                    var cid = this.id;
                    $('#comfield'+cid).empty();
                    $('#comfield'+cid).load('content_comment.php?id=55&cid=' + cid);
                });
            });
        });

        $("#submitcomment").click(function(){
            $('#formcomment').ajaxForm(function (data, textStatus){
                $('#formcomment').clearForm();
                $('#comfield2').empty();
                $('#comfield2').html('<input class="ce" id="2" type="text" value="Write a comment..." readonly="readonly" />');
                $('#comdata2').append(data).fadeIn(700);
                var ajaxInProgress = false;
                $(function(){
                    if (ajaxInProgress) return;
                    $('.ce').focus(function(){
                        var cid = this.id;
                        $('#comfield'+cid).empty();
                        $('#comfield'+cid).load('content_comment.php?id=55&cid=' + cid);
                    });
                });
            });
        });

    });
</script>

<form id="formcomment">
    <textarea id="commenttext2>" name="comment" class="commentarea"></textarea>
    <input type="submit" id="submitcomment" value="comment />
    <button id="cancelcomment2">Cancel</button>
</form>

Upvotes: 0

Views: 647

Answers (1)

Kevin B
Kevin B

Reputation: 95022

This may not be the "best" way to do it, but it will work.

replace all occurences of

$('.ce').focus(function(){

with

$('.ce').unbind("focus").focus(function(){

in both scripts.

The better way would be event delegation, though that will take more changes to the code.

Upvotes: 2

Related Questions