I have the following code:
<input class="span7" type="text" id="search" name="search">
<div class="well" id="search-results" style="position: absolute;
top: 208px;
left: 62px;
height: 100px;
width: 340px;
overflow:
auto;">
<ul id="list-results" style="margin: 0; padding:0; top: 0;">
{% include 'search-results.html' %}
</ul>
</div>
The search-result.html is displayed via ajax, when a keyup is done in an input field. An ajax request appends into the ul an element like this:
<li id="{{article.id}}"><a id="result-selected" href="#">{{ article.title }}</a></li>
When I use this function works (it's displaying in the console):
$('#list-results').click(function(e){
console.log($(this));
});
When I select one of these instead, it doesn't:
$('#list-results ul')
$('#list-results > ul')
$('#result-selected')
and more...
Is this a problem that this html is rendered by ajax but it's not at the begging when the page is loaded? What can I do? I want to take the values of the displayed a to do something.
Ajax call:
$('#search').keyup(function(){
$.ajax({
type: "POST",
url:"/meal/search/",
data:{
'search_text': $('#search').val(),
'csrfmiddlewaretoken': $('input[name=csrfmiddlewaretoken]').val()
},
success: searchSuccess,
dataType: 'html'
}
);
});
function searchSuccess(data, textStatus, jqXHR){
if ($('#search').val().length < 3){
$('#search-results').hide();
} else {
$('#search-results').show();
}
$('#list-results').html(data);
}
ajaxcall? It would be better if we could see it.