0

I have source code run by tomcat use ajax get data.

<script type="text/javascript">
            var captureEvent= 0;
            var number= 0;
            $(document).ready(function(){
                $("#searchBox").keyup( function(event){
                    if(event.which == 50) {
                        captureEvent = 1;
                    }
                    if(number == 10){ 
                        number = 0;
                        captureEvent = 0;
                    }
                    if(captureEvent == 1){
                        number++;
                        var searchText = $(this).val();
                        var dataString = 'searchword='+ searchText;
                        if(searchText == ''){}
                        else{
                            if(searchText.length != 1){
                                $.ajax({
                                   type: "GET",
                                   url: "../taglikefacebook",
                                   data: dataString,
                                   cache: false,
                                   success: function (html) {
                                       $("#display").html(html).show();
                                   }
                                });
                            }
                        }
                    }
                });
            });
        </script>
        <div>
            Search: <input type="text" class="search" id="searchBox"/>
        </div>
        <div id="display" style="  float: left;"></div>
        <script type="text/javascript">
            $(".display_box").click( function(){
                    number = 0;
                    captureEvent = 0;
                    $("#searchBox").val($(this).attr('name'));
                    $("#display").empty();
                });
        </script>

When i get data for id= display . Code html like that:

            <div>
                Search: <input type="text" class="search" id="searchBox">
            </div>
            <div id="display" style="float: left; display: block;">
                <div class="display_box" align="left">
                    <img src="img/Hot.gif">Demo&nbsp;1<br>2010-2015</div>
                <div class="display_box" align="left">
                    <img src="img/Hot.gif">Demo&nbsp;2<br>2010-2015
                </div>
                <div class="display_box" align="left">
                    <img src="img/Hot.gif">Demo&nbsp;3<br>2015-2015
                </div>
            </div>

But when i check in browser function $(".display_box").click(); not work when i click div tag id=display_box. I don't understand why? Solution about this? Somebody help me!

Nick
  • 1,417
  • 1
  • 14
  • 21
Yên Đậu
  • 141
  • 1
  • 9
  • I just dont understand what's the meaning of this line of code: if(searchText == ''){} – xsami Apr 02 '15 at 15:53
  • Do you realize you can not attach events to things that do not exist? It is like trying to eat a pizza before you order it. It needs to be there in your hands before you can bite into the slice. – epascarello Apr 02 '15 at 15:56
  • `$("#display").on)("click", ".display_box", function(){ alert("Event Delegation is awesome!"); });` – epascarello Apr 02 '15 at 15:58
  • @epascarello hey. I want get name when click tag has class= .display_box but function $(this).attr('name') not work. Do you know solution about this. Thank you very much! – Yên Đậu Apr 02 '15 at 16:47
  • There is no name attribute on the "display_box" element in the code you posted. – epascarello Apr 02 '15 at 16:54
  • @epascarello oh i'm so sorry but i had delete that comment but it didn't work. Thank you for support to me! – Yên Đậu Apr 03 '15 at 02:18

0 Answers0