2

We have a search functionality in our application, when user types in something, the result will be generated based on the filter and displayed on page.

Here's the dynamic result after each keyup:

htmlResult = `
<li>
    <div>
        <img src='${user.avatar}' width='80' alt='${user.fullName}' />
    </div>
    <a tabindex='0'
        class ='btn2'
        data-html='true'
        data-toggle='popover'
        data-content="<div class='list-group'><a href='#' class='item' data-val1-value='0'>Val1</a></div>">add</a>
</li>
`;

Here are the event handlers:

$("[data-toggle=popover]").popover();
$(document).on('click', '.btn2', function(event) {
    event.preventDefault();
    $(this).popover({ html: true });
});

$(document).on('click', '.item', function (event) {
    event.preventDefault();
    const value1 = $(this).attr('data-val1-value');

    const params = {
        value1: value1
    };
    $.ajax({
        type: "POST",
        url: endPoint,
        dataType: 'json',
        contentType: 'application/json',
        data: JSON.stringify(params),
        success: (result) => {
            // refresh
        }
    }); 
});

There are two problems with this code:

  • popover will be displayed after 3 times clicking on .item
  • jQuery ajax won't succeed, it shows 400 Bad Request error on console.

PS: this code works fine with not-generated markup.

any idea?

Sirwan Afifi
  • 10,654
  • 14
  • 63
  • 110

2 Answers2

0

a) to make sure your url is correct, 1) update your serverside funtion not to accept any parameter just for testing 2) call ajax with data: NULL 3) debug and see if it reaches to server side function.

b) if not reached to server side, check if it is a POST or GET in server side function. c) if you are sure that server side function is correctly called, data without stringfy.

that might help.

Shwe
  • 455
  • 5
  • 11
0

For popover you must reinitiate every your dynamic content was append to HTML. For Ajax 400 this is because the URL not found the route path, please check again your backend requirement for this request.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
mazipan
  • 183
  • 2
  • 10