0

When I execute the below code all the img with the id of imgg will be replaced because they are in a foreach loop, but I want to apply that to the clicked one only. Can any one help?

Html:

<button type="submit" id="getRequest" class="btn btn-info btm-sm " role="button" style="width:100px;height:30px">
<p id="imgg">Add to Cart</p>
 </button>

JS:

   $(document).ready(function() {
$(document).on('submit', '#reg-form', function() {

    var data = $(this).find("#post_id").val();
    //var ln = $("#lname").val();

    //var data = 'fname='+fn+'&lname='+ln;

    //  var data = $(this).serialize();
    $.ajax({
        type: 'POST',
        url: '{{url("/ajax")}}',
        data: {
            'name': data,
            '_token': $('input[name=_token]').val()
        },
        success: function(data) {
            $(imgg).replaceWith('<img id=imgg   src=img/ajax.gif> ');

            setTimeout(function() {
                $(imgg).replaceWith('  <p id="imgg">Add to Cart</p> ').hide('blind', {}, 500)
            }, 1300);
            console.log(data);
        },

        error: function(data) {
            alert("You must Login First");
        }
    });
    return false;
});

});

amanuel2
  • 4,508
  • 4
  • 36
  • 67
Achraf Khouadja
  • 6,119
  • 4
  • 27
  • 39

2 Answers2

-1

Thanks everyone i found the solution But you da real MVP, here it is

 $(document).ready(function()
{
 $(document).on('submit', '#reg-form', function()
 {
var imgid = $(this).find("#imgg");
var data = $(this).find("#post_id").val();
  //var ln = $("#lname").val();

  //var data = 'fname='+fn+'&lname='+ln;

//  var data = $(this).serialize();
  $.ajax({
  type : 'POST',
  url  : '{{url("/ajax")}}',
 data: {'name':data, '_token': $('input[name=_token]').val()},
 success: function(data) {
    $(imgid).replaceWith('<img id=imgg src=img/ajax.gif> ');
   var thisForm = this;
    setTimeout(function() {
        $(imgg).replaceWith('  <p id=imgg>Add to Cart</p> ').hide('blind', {}, 500)
    }, 1300);
    console.log(data);
},

      error :  function(data)
      {
          alert("You must Login First");
      }
  });
  return false;
 });

});
</script>
<script type="text/javascript">
$.ajaxSetup({
   headers: { 'X-CSRF-Token' : $('meta[name=_token]').attr('content') }
});
Achraf Khouadja
  • 6,119
  • 4
  • 27
  • 39
-2

Instead of using the $(imgg) use $(this), and you also missed the quotations when you where giving the id of iamge when you where using .replaceWith:

success: function(data) {
    $(this).replaceWith('<img id="imgg" src="img/ajax.gif>" ');
   var thisForm = this;
    setTimeout(function() {
        $(thisForm).replaceWith('  <p id="imgg">Add to Cart</p> ').hide('blind', {}, 500)
    }, 1300);
    console.log(data);
},

Reason?

Simple. The this object doesn't change. It is the owner of the function. See more about why here: Jquery - When to use "this" and when to use "$(this)"?

EDIT2

Now i have to put that in the upper level so this:

  $(document).ready(function() {
     $(document).on('submit', '#reg-form', function() {

         var vm = this //Upper Level
         var data = $(this).find("#post_id").val();
         $.ajax({
             type: 'POST',
             url: '{{url("/ajax")}}',
             data: {
                 'name': data,
                 '_token': $('input[name=_token]').val()
             },
             success: function(data) {
                 $(vm).replaceWith('<img id=imgg   src=img/ajax.gif> ');

                 setTimeout(function() {
                     $(vm).replaceWith('  <p id="imgg">Add to Cart</p> ').hide('blind', {}, 500)
                 }, 1300);
                 console.log(data);
             },

             error: function(data) {
                 alert("You must Login First");
             }
         });
         return false;
     });
 });

Please refer here about this particular problem:
Uncaught TypeError: Cannot read property 'createDocumentFragment' of undefined

Community
  • 1
  • 1
amanuel2
  • 4,508
  • 4
  • 36
  • 67