0

I'm developing a Laravel project and I'm facing an issue with Ajax and Blade template. In my scenario I have several tag (it can be divs, buttons or list items) with a class ajaxaction and an attribute data-route, like this:

      <button type="button" class="ajaxaction" data-route="http://localhost/webora/public/d/5106">
      <button type="button" class="ajaxaction" data-route="http://localhost/webora/public/d/5105">
      <button type="button" class="ajaxaction" data-route="http://localhost/webora/public/d/5104">
      <button type="button" class="ajaxaction" data-route="http://localhost/webora/public/d/5103">
      <button type="button" class="ajaxaction" data-route="http://localhost/webora/public/d/5102">

and a tag wrapping the content i will replace:

<div class="content">

</div>

In this page I have the script like this:

$('.ajaxaction').click(function () {
  ajaxURL = $(this).attr('data-route');
  $.get( ajaxURL, function(data) {
    $('.content').html(data);
  })
  .fail( function(jqXHR, textStatus, errorThrown) {
    $('.content').html(textStatus + ' ' + errorThrown);
  });
});

As I expect every time a tag is clicked the ajax send a GET request that return an HTML that replace the previous in content. In some case the content replaced has a tag with class ajaxaction itself like this:

<div class="content">
    ...
    <button type="button" class="ajaxaction" data-route="http://localhost/webora/public/d/5199">
    ...
</div>

But clicking on this tag do not send any ajax request.

2
  • 2
    in case you have a time, "event delegation" is a must read topic here Commented Nov 30, 2017 at 14:34
  • 1
    Use the .on() method to delegate the click event to (future) elements dynamically added to the DOM: stackoverflow.com/questions/15420558/… Commented Nov 30, 2017 at 14:36

2 Answers 2

0

You might want to consider this jquery ajax solution.

from your existing code.

    $('.ajaxaction').click(function () {
  ajaxURL = $(this).attr('data-route');
  $.get( ajaxURL, function(data) {
    $('.content').html(data);
  })
  .fail( function(jqXHR, textStatus, errorThrown) {
    $('.content').html(textStatus + ' ' + errorThrown);
  });
});

<div class="content">
    ...
    <button type="button" class="ajaxaction" data-route="http://localhost/webora/public/d/5199">
    ...
</div>

to this one. since you wrap your buttons already in your div. You create a script like this.

$('div.content').on(".ajaxaction","click",function(){
   var _btn = $(this);  
   $.ajax({
      url : _btn.data('route'),
      success : function(data){
          //your success data here

      },
      error : function(jqXHR,textStatus,thrownError){
        console.log(jqXHR); 
     } 
   })
});

don't use the attr('data-xxxx') because once they change the value of your data-xxxx, it will reflect to your code and run the code with the changes unlike with data('xxxx'), the initial value will still be the value even if they change it.

Sign up to request clarification or add additional context in comments.

2 Comments

As of jQuery 3.0, .delegate() has been deprecated. It was superseded by the .on() method since jQuery 1.7, so its use was already discouraged. api.jquery.com/delegate
on that case , you can just update the function delegate to 'on' and still work ;) right ?
0

I mark as answered my question because the solution provided by @Chay22 and @Mehdi Alipour in their comments is exactly what I need to know.

event-delegation

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.