Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7893591
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T07:10:17+00:00 2026-06-03T07:10:17+00:00

I have a page that is posting a JSON to a cfc, and getting

  • 0

I have a page that is posting a JSON to a cfc, and getting a success/fail message back. The array that the JSON is built from contains the ID’s of the input fields that were updated. I am changing the background color of the elements to alert the users that an update was made, but the problem arises when the the user posts more changes without leaving the page. The array still contains the data from the previous post, and no matter what or where I try to clear the array, the changing of the background stops working.

Here is the code:

$("input").change(function(){
    theArray.push({
       'id':$(this).attr('id'),
       'value':$(this).val()
    });

});

$("#edit_button").click(function(){
    if(theArray.length >0){
        theArray.push({
           //some processing information to the cfc
        });
        theJson = JSON.stringify(theArray);
        $.post("CFCs/fund_profile.cfc",
            {
                method:"updateProfile",
                data:theJson
            },
            function(data){
            if(data.HASERROR == 1){
                $("#messageDiv").empty().html(data.MESSAGE);
            }
            else{
                $("#messageDiv").empty().html(data.MESSAGE);
                theArray.pop();//removing the cfc processing information
                for(var i = 0; i <= theArray.length; i++){
                    var $el = $("#" = theArray[i].id).parent().parent().css('background','red');
                    setTimeout(function(){
                        $el.css('background','');
                        $("#messageDiv").empty();
                    },5000);
                }
            }
         },
         "json");
        //THIS IS WHERE THE PROBLEM LIES (I THINK)
        //I HAVE TRIED THE FOLLOWING:
        var arrayLen = theArray.length;
        for(var j = 0;j <= arrayLen; j++){
            theArray.pop();
        }//DOESN'T WORK

        theArray.length = 0;
        //DOESN'T WORK

        for(var j = 0;j <= arrayLen; j++){
            theArray.shift();
        }//DOESN'T WORK
    }
});

If I remove the code to clear the array, the background change happens, but the array never loses that element, and will always show that element as being updated. If I keep it the background changes doesn’t happen at all.

I”m sure it is something simple that I am missing, but I’m frustrated about it.

Any thoughts?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T07:10:19+00:00Added an answer on June 3, 2026 at 7:10 am

    Add this inside of your post statement after the if/else statement:

    theArray = [];
    

    That effectively makes the array empty. No need for looping through it.

    Also, as pointed out by David, it has to be inside of the success of the post, not after it.

    Edit:
    Your for loop isn’t executing exactly as you expect it to, the setTimeout should only affect the last element. To solve that, switch to a $.each, or create an anonymous function inside of your for loop.

    for(var i = 0; i <= theArray.length; i++){
        (function(i){
            var $el = $("#" = theArray[i].id).parent().parent().css('background','red');
            setTimeout(function(){
                $el.css('background','');
                $("#messageDiv").empty();
            },5000);
        })(i);
    }
    

    One more note:
    If someone submits a second set of data to the server before the first one is complete, things may get very messed up due to both cases using the same array. David touched on that on his answer pretty well. Maybe on the click event you should immediately make a copy of the array and then clear the original, leaving it open for use by the next click.

    Example (possibly working, not tested):

    var theArray = [];
    $("input").change(function() {
        theArray.push({
            'id': $(this).attr('id'),
            'value': $(this).val()
        });
    
    });
    
    $("#edit_button").click(function() {
        var myArray = $.extend([], theArray, true); // get a copy of theArray
        theArray = []; // empty theArray
        if (myArray.length > 0) {
            myArray.push({
                //some processing information to the cfc
            });
            theJson = JSON.stringify(myArray);
            $.post("CFCs/fund_profile.cfc", {
                method: "updateProfile",
                data: theJson
            }, function(data) {
                if (data.HASERROR == 1) {
                    $("#messageDiv").empty().html(data.MESSAGE);
                }
                else {
                    $("#messageDiv").empty().html(data.MESSAGE);
                    myArray.pop(); //removing the cfc processing information
                    var $el = $([]);
                    for (var i = 0; i <= myArray.length; i++) {
                        $el.add("#" = myArray[i].id);
                    }
                    $el = $el.parent().parent().css('background','red');
                    setTimeout(function(){
                        $el.css('background','');
                        $("#messageDiv").empty();
                    },5000);
                }
            }, "json");
        }
    });
    

    Edit updated for loop in last statement, should be easier to understand.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a web page that uses cross page posting to post to a
i am porting over a website from asp. i have one page that i
I have a page that, depending on input from a previous page, lists a
I have a page that makes use of the jTweetsAnywhere code (from here )
I have a page that is dynamically built by the database. For each thing
I have Results page that contains an signup form. I'm trying to use client-side
I have this page that I'm going a CURL POSTing some info to another
I currently have code for posting data to a login page that works fine.
I have a page that takes a few minutes to run. When I set
I have a page that opens a modal dialog. After the operations done on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.