1

It's difficult to explain but i'll do my best. 1 - I want to build a search bar that, on the jquery event keypress, searches in my database what the user has digited. 2- Then i want to fill a div with the results found. 3 - then i want to click or tap the value and see the value tapped or click filling the search bar.

No problems with step 1 and 2, because using jquery $.post() method all works fine but i cannot complete step 3, because i generate an html in my php file that, by the callback function of $.post method, fill the div. The problem is that jquery seems not able to catch the class i inserted into php generated file. So my question is: what's the proper way to achieve my goal? I hope i explained well ;)

  • Are you talking about general taxonomy, and autocomplete? there are myriad plugins for jQuery for this out there, no need to reinvent the wheel. As for not catching the elements you add via the returned `$.post`-method, I'm guessing you've just used `$('.something').click()` to catch the clicks? That won't work on dynamically generated content. Use `$('.something').on('click',function()` instead. – junkfoodjunkie Jan 10 '17 at 17:21
  • 1
    Do you mean that the click event is not being registered? If so, this could be because you are registering the click event to the dom before that element exists. $(document).on('click', element, callback) may solve this for you.. – Pete Gaulton Jan 10 '17 at 17:23
  • @junkfoodjunkie `.on('click', function()` is the exact same as `.click(function()` – 4castle Jan 10 '17 at 17:28
  • @4castle I think you're right and I'll try that. Thank you all for answering! – Tech Inspector Jan 10 '17 at 17:47
  • No, it isn't. Or, rather, yes, in that example, it is, but if that element you use as a selector is a dynamically created element: `$('#dynamic').on('click',function()` will actually target that element, while `$('#dynamic').click(function()` does absolutely nothing. `.on('click')` does what `.click()` does, but it **also** does what `.bind()` and `.live()` does. – junkfoodjunkie Jan 11 '17 at 01:59

0 Answers0