-4

How to do a doOnce function?

I have this jQuery script and I want to add a doOnce function to it. I want to click a tag and submit this function database once, but when I do it update the last updated insert not the current one.

Any help would be great.

$("document").ready(function() {
    $(".edit_happening").click(function() {
        var element = $(this);
        var edit_id = element.attr("id");
        var edit_title = $("#edit_title<?php echo $id; ?>").val();
        var edit_start_time = $("#edit_start_time<?php echo $id; ?>").val();
        var edit_end_time = $("#edit_end_time<?php echo $id; ?>").val();
        var edit_description = $("#edit_description<?php echo $id; ?>").val();
        var edit_location = $("#edit_location<?php echo $id; ?>").val();
        var info = "edit_id=" 
            + edit_id 
            + '&edit_title=' 
            + edit_title
            + '&edit_start_time=' 
            + edit_start_time 
            + '&edit_end_time=' 
            + edit_end_time 
            + '&edit_description=' 
            + edit_description 
            + '&edit_location=' 
            + edit_location;

        if (edit_title == 'Edit Happening') {
            $("#errorMsg_edit<?php echo $id; ?>").fadeIn(200).show();
        }
        else if (edit_description == '') {
            $("#errorMsg_edit<?php echo $id; ?>").fadeOut(200).hide();
            $("#errorMsg2_edit<?php echo $id; ?>").fadeIn(200).show();
        }
        else {
            $.ajax({
                type: "POST",
                url: "/happening_edit.php",
                data: info,
                cache: false,
                success: function(html) {
                    $("#title<?php echo $id; ?>").html(edit_title);
                    $("#happening_edit_info<?php echo $id; ?>").fadeOut(200).hide();
                    $("#happening_info<?php echo $id; ?>").fadeIn(200).show();
                    $("#happening_msg_edit<?php echo $id; ?>").show();
                    setTimeout(function() {
                        $("#happening_msg_edit<?php echo $id; ?>").fadeOut(200).hide();
                    }, 3000);
                }
            });
        }
        return false;
    });
});​
3
  • Do you want the click event to only occur once per element with class of "edit_happening"?
    – kendaleiv
    Mar 3, 2012 at 5:44
  • yes that what i'm trying to do, and be able to do it again to a different selection.
    – Kasagi
    Mar 3, 2012 at 5:53
  • 1
    Possible duplicate of your other question stackoverflow.com/questions/9487849/… Mar 3, 2012 at 5:54

1 Answer 1

1

Need more details to accurately answer, but:

http://api.jquery.com/one/

$( '#something' ).one( function(){
   ...
} );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.