Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I stop multiple posts in my script?
    primarykey
    data
    text
    <p>I've written a rudimentary text editor for my page. Any tag with the .edit class can be clicked by a logged in user and a form appears with the text in the textarea field to be edited in addition to unbinding the 'click'. Upon submit the function performs a post and updates the database via a ColdFusion component and reloads the page to reflect the changes made. Works great (thanks in no small part to the Stackoverflow community and Ray Camden).</p> <p>I created a 'cancel' function that clears the form field (because it's reusable) and re-binds teh 'click'. The trouble is, I see in firebug that if I click some text, cancel, click some other text and then click submit my code performs two posts - one for the cancelled click and one for the real thing.</p> <p>What do I need to do to kill that first aborted effort?</p> <p>Here's the code:</p> <pre><code> $(document).ready(function() { $('#cancelEdit').click(rebinder); $('.edit').click(texteditor); function texteditor(){ var theName = $(this).attr('name'); var textToEdit = $(this).html(); var theParentID = $(this).closest('div').attr('id'); var theTag = this.tagName.toLowerCase(); var theID = theName.split("-")[1]; var gateway = ("&lt;cfoutput&gt;#URL.page#&lt;/cfoutput&gt;"); var fieldDims = [$(this).css('width'), $(this).css('height')]; $('#theText').attr('value', textToEdit ); $('#theText').css('height', fieldDims[1]); $('#theText').css('width', fieldDims[0]); $('.edit').unbind('click'); $('#texteditform').submit(function(e){ //stop the form submission e.preventDefault() var newText = $('#theText').val(); //CFC $.post("cfc/engine.cfc?method=updateText&amp;returnformat=json", {text:newText, field:theID, gateway_id:gateway}, function(res) { //Handle the result if(res.trim() == "true") { $('#theText').attr('value', ''); location.reload(); //$('.edit').bind('click'); } else { alert("Didn't Work"); } }); }); }; function rebinder(){ alert('rebinder'); $('.edit').click(texteditor); $('#theText').attr( 'value', '' ); $('#theText').css('height', ''); $('#theText').css('width', ''); }; // End of document.ready function // }); </code></pre> <p>I just looked at the console and not only does the post happen twice but it posts the text from the second attempt to both post efforts which, unfortunately updates both elements with the .edit class. That's bad...</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload