Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

130
Views
Array didn't update after array filter

After i click the remove image if the array have 7 item one remove and i click another it remove another one item but array still 6 it didn't update the last one i delete how can i solve it thanks you for ur time

Jquery

$(document).on('click', '.remove-image', function() {
        var id = $(this).attr('id');
        var product_id =$(this).attr('product_id');
        var name =$(this).attr('name');
        var array=$(this).attr('array');
        var arr = JSON.parse(array);
        // confirm("Are You sure want to delete !");
        var arr = arr.filter(val => val !== name);
        console.log(arr);
        var formData = {
            id: id,
            product_id : product_id,
            image: arr
        };

        // $.ajax({
        //     type: "POST",
        //     url: "{{ url('image/delete') }}",
        //     data: formData,
        //     success: function () {
        //         alert('workinggggg');
        //     },
        //     error: function () {
        //         alert('errorrrrrrrr');
        //     }
        // });

        $(this).parent('div').remove();
    });

Laravel Blade

  @foreach (json_decode($gallery->image) as $key => $image)
                    <div>
                        <span name="{{ $image }}" id="{{ $gallery->id }}" product_id="{{ $product->id }}" array="{{ $gallery->image }}"
                            class="flex text-red-400 pl-3 image cursor-pointer remove-image">X</span>
                        <img src="{{ asset('storage/' . $image) }}" class="pr-5" alt="{{$product->slug}}">
                    </div>
                    {{ var_export($key) }}
                    @endforeach

enter image description here

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Your code is correctly gets a filtered array, but it doesn't write the result back to the attribute, and so the next time that code executes, the array is built again from the attribute's value, which still has the original JSON.

So add one statement to this part:

    var array = $(this).attr('array');
    var arr = JSON.parse(array);
    var arr = arr.filter(val => val !== name);
    $(this).attr('array', JSON.stringify(arr));   // <--- 
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!