tags:

views:

706

answers:

2

I have a page that has a bunch of JQuery objects. On that same page, I have a form where you can enter search terms in a series of fields and click on a search button, which will then send an AJAX request to the server and then load the resulting HTML into a div. A very simply Jquery application...

The problem is this. When you click on a search result link that was loaded into the div via ajax, the Jquery code on the original page will not fire. If I add a function called searchClick() on the original page, for example, then I add an onClick="searchClick()" to each <a> tag that is loaded into the div via ajax, clicking on that link yields a JS error that says searchClick() is undefined. But it is defined! I defined it before the AJAX call. I was reading up on Jquery event bubbling but this doesn't seem like the problem. I am not sure actually but I have tried it and it still didn't work. here is my current code

on the main page that loads when the browser loads the page:

<script language="javascript" type="text/javascript">

$(document).ready(function() {

...

    **function searchClick(value)** {
      alert("here");
    }

     $('#search').click(function() {
             description = urlencode($("#description").val());
             manufacturer = urlencode($("#manufacturer").val());
             product = urlencode($("#product").val());
             category = urlencode($("#category").val());
             subcategory = urlencode($("#subcategory").val());

             $(**'#search_results'**).**load**('/productSearch.php?description=' + description + '&product=' + product + '&category=' + category + '&subcategory=' + subcategory')

       });

    ...
    </script>

    <div id="**search_results**"></div>

the following is an example of the links that appear in the search_results div when the ajax query is called (the load())

<a href="#" onclick="s**earchClick("some value")"**>Product 1</a>

Clicking on that link in the example above will yield a searchClick() not defined error. Anyone got any suggestions? Thank you!

+2  A: 

At first live events will make your life easier, as you won't have to add the onclick handler yourself. Instead you can bind an event handler that will also apply to elements later added to the DOM (through AJAX calls for example).

As for your problem, I have no explanation. Try to monitor the html source as well the DOM structure using a tool like Firebug.

kgiannakakis
A: 
$("#search").live("click", function()
{
    var description = urlencode($("#description").val());
    var manufacturer = urlencode($("#manufacturer").val());
    var product = urlencode($("#product").val());
    var category = urlencode($("#category").val());
    var subcategory = urlencode($("#subcategory").val());

   $.ajax
   ({
        type: "GET",
     url: "/productSearch.php",
     data: 'description=' + description + '&product=' + product + '&category=' + category +"&subcategory=" + subcategory,
     success: function(result)
     { 
      $("#search_result").html(result);
     }
    });
});

Use JQuery Live events when new elements are added they will still react to events. Don't use

<a href="#" onclick="s**earchClick("some value")"**>Product 1</a>

its bad form.

MrHus