shahid khan shahid khan - 3 months ago 5
jQuery Question

Selecting data from ajax responded div using jquery

I am working on making hashtag system based on codeigniter for that i need to select data from ajax returned value which work based on input given through text box i tried to alert the value from jquery but alert box has no value or empty

controller upload has function hastag

public function hashTag()
{
$test = $this->input->post('search');
echo $test;
}





<div class="form-group col-sm-9">
<div class="col-sm-6">
<input type="text" class="form-control"
id="inputPjctTag" name="pjctTag" placeholder="">
</div>
<div class="col-sm-6" id="display">
</div>
</div>


javascript is

<script type="text/javascript">
$( "#inputPjctTag" ).keypress(function() {
$.ajax({url: "http://localhost/ci/upload/hashTag",data:'search='+$("#inputPjctTag").val(),type:"POST", success: function(result){
$('#display').html(result);
$('#inputPjctTag').prepend(result);
}});
});
$( "#display" ).click(function() {
var resul = $('#display').val();
alert(resul);
$("#inputPjctTag").prepend(data)
});

</script>


i dont know what is wrong please help me

Answer

A div tag never has a value as it has only text and html.

 $( "#display" ).click(function() {
    var resul = $('#display').val();
    alert(resul);
    $("#inputPjctTag").prepend(data)
});

Change it with this:

 $( "#display" ).click(function() {
    var resul = $('#display').text(); //Or $('#display').html() for html
    alert(resul);
    $("#inputPjctTag").prepend(data)
});