0

I have a search product page in my application. I use a jQuery ajax function for databind product list. There is a compare checkbox in each item in product list.

enter image description here

Please see my Product list html below.

Rendered Html

    <div id="productsContainer">
                            <div class="product_list1">
                                <div class="product_image">
                                    <img src="http://www.barco.com/~/media/Images/Products/2011/3D%20display%20management/XDS-150%20%20200%20%20400.jpg">
                                </div>
                                <div class="product_desc">
                                    <h1>TransForm XDS-1100</h1>
                                    <p>Universal and scalable collaboration solution for a 3D stereo display</p>
                                    <div class="add_compare">
                                        <input id="test" 0'="" _id="55a7624778a72623f843d778" cmp="compare" type="checkbox">
                                        <label for="test" 0'="">Add to compare</label>
                                        <div>
                                        </div>
                                        <div class="clear"></div>
                                    </div>
                                </div>
                                <div class="clear"></div>
                            </div>
                            <div class="product_list1">
                                <div class="product_image">
                                    <img src="http://www.barco.com/~/media/Images/Products/Q%20-%20T/TransForm%20N/Transform%20N01NGP324jpgnewer2jpg.jpg">
                                </div>
                                <div class="product_desc">
                                    <h1>TransForm ECU-110</h1>
                                    <p>Preconfigured and easy to use high-res video wall controller</p>
                                    <div class="add_compare">
                                        <input id="test" 0'="" _id="55a75eac78a72623f843d76f" cmp="compare" type="checkbox">
                                        <label for="test" 0'="">Add to compare</label>
                                        <div>
                                        </div>
                                        <div class="clear"></div>
                                    </div>
                                </div>
                                <div class="clear"></div>
                            </div>

                            <div class="clear"></div>
                        </div>

Script

$("#productsContainer .product_list1 .product_desc .add_compare").on("click", "input:checkbox", function () {

    alert('message ');

});

Edit

Html in initial stage

 <div id="productsContainer">

 </div>

Json Result

    public JsonResult GetSearchedProducts(SearchProductModel model)
    {
        _productHelper = new ProductHelper();

        var data = _productHelper.GetSearchedProducts(model);

        StringBuilder strbld = new StringBuilder();

        int i = 0;

        foreach (var item in data)
        {

            strbld.Append("<div class=\"product_list1\">");
            strbld.Append(" <div class=\"product_image\"><img src=" + item.Picturepath + " /></div>");
            strbld.Append(" <div class=\"product_desc\">");
            strbld.Append("     <h1>" + item.Name + "</h1>");
            strbld.Append("     <p>" + item.Shortdesc + "</p>");

            strbld.Append("     <div class=\"add_compare\">");
            strbld.Append("         <input id='test'" + i + "' _id=" + item._id.ToString() + "  cmp='compare' type=\"checkbox\"/>");
            strbld.Append("         <label for='test'" + i + "'>Add to compare</label>");
            strbld.Append("         <div>");

            strbld.Append("         </div>");
            strbld.Append("         <div class=\"clear\"></div>");
            strbld.Append("     </div>");

            strbld.Append(" </div>");
            strbld.Append(" <div class=\"clear\"></div>");
            strbld.Append(" </div>");

        }

        strbld.Append("<div class=\"clear\"></div>");

        return Json(strbld.ToString(), JsonRequestBehavior.AllowGet);
    }

When I click the checkbox, it's not fired. How can I fix this?

halfer
  • 18,701
  • 13
  • 79
  • 158
Ragesh S
  • 3,691
  • 12
  • 92
  • 128
  • can you make a fiddle for this, then the question will be easy to answer. – Vinay Pratap Singh Sep 08 '15 at 09:21
  • Thank you for your valuable reply. Actually html was added in this div with id 'productsContainer'. Please see my edited question. – Ragesh S Sep 08 '15 at 09:30
  • Just do `$("#productsContainer").on("click", ".product_list1 .product_desc .add_compare input:checkbox", function () {` it should work fine. Also refer to the original answers for more details.. – palaѕн Sep 08 '15 at 09:41
  • But this code is only fired the first checkbox only. other checkbox not working. – Ragesh S Sep 08 '15 at 10:05
  • 1
    @RageshPuthiyedath: ok, please delete this question and ask a new question once again to get more attention to your query. Do mention that its working for one checkbox but not for others and the main thing that you have already implemented [**Event Delegation**](http://learn.jquery.com/events/event-delegation/) as above but its not working in your case. Also paste the link of the new question here so that I can also view the issue and see what other users have to say about it. – palaѕн Sep 08 '15 at 12:22

0 Answers0