0

I don't know a lot about jQuery, and I want know how do I reload all the functions of jQuery after a ajax call, because I add element in a and then the new element don't work but the others work fine.

The ajax call:

$(document).ready(function () {

    $('#search-bar').keyup(function (event) {
        if (event.keyCode == 13) {
            myFunction();

        }
    });

        var city = $('#search-bar').val();
        $.ajax({
            url: '@Url.Action("getWeatherSearch", "Index")',
            data: { city: city },
            //async: false,
            error: function (resp) {
                var div = document.getElementById('rb-grid');
                div.innerHTML = resp.responseText + div.innerHTML;
            },
            success: function (resp) {
                $("#rb-grid").prepend(resp);
            }
        });

HTML:

The textbox: @Html.TextBox("search-bar", "", new { @class = "search-bar", placeholder = "Pesquisar" })

This is where I wnat to add the new

  • that I create dinamically, calling the method from c# using the ajax:
            <ul id="rb-grid" class="rb-grid clearfix">
    
                <li class="icon-clima-1 rb-span-2">
                    <h3>@ViewBag.CurrentCity</h3><span class="rb-temp">@ViewBag.CurrentTemp°C</span>
                    <div class="rb-overlay">
                        <span class="rb-close">close</span>
                        <div class="rb-week">
                            <div><span class="rb-city">@ViewBag.CurrentCity</span><span class="icon-clima-1"></span><span style="font-size: 60px !important;">@ViewBag.CurrentTemp ºC</span><span style="font-size: 40px !important;">Min: @Next0.tempMinC ºC</span></div>
                            <div style="width: 17.5%;"><span>@Next1.date.DayOfWeek.ToString().Substring(0, 3)</span><span class="icon-clima-1"></span><span style="font-size: 40px !important;">Max: @Next1.tempMaxC ºC <br /><br /> Min: @Next1.tempMinC ºC</span></div>
                            <div style="width: 17.5%;"><span>@Next2.date.DayOfWeek.ToString().Substring(0, 3)</span><span class="icon-clima-1"></span><span style="font-size: 40px !important;">Max: @Next2.tempMaxC ºC <br /><br /> Min: @Next2.tempMinC ºC</span></div>
                            <div style="width: 17.5%;"><span>@Next3.date.DayOfWeek.ToString().Substring(0, 3)</span><span class="icon-clima-1"></span><span style="font-size: 40px !important;">Max: @Next3.tempMaxC ºC <br /><br /> Min: @Next3.tempMinC ºC</span></div>
                            <div style="width: 17.5%;"><span>@Next4.date.DayOfWeek.ToString().Substring(0, 3)</span><span class="icon-clima-1"></span><span style="font-size: 40px !important;">Max: @Next4.tempMaxC ºC <br /><br /> Min: @Next4.tempMinC ºC</span></div>
                        </div>
                    </div>
                </li>
    

    this is how I activate Boxgrid:

    <script>
        $(function () {
            Boxgrid.init();
        });
    </script>
    
  • Robert
    • 8,521
    • 12
    • 58
    • 108
    Vinicius
    • 61
    • 1
    • 9

    3 Answers3

    1

    Initialize once again in your ajax callback

     success: function (resp) {
         Boxgrid.init();
     }
    
    Murali Murugesan
    • 21,303
    • 16
    • 65
    • 114
    1

    What you need to do is add the capacity to bind events to newly added DOM elements; your code currently is only being bound to current DOM elements.

    Instead, use $.on() to do something like:

    $(<selector>).on(<event>, <callback).

    Jonline
    • 1,499
    • 2
    • 20
    • 49
    0

    Are you using a viewModel? If so you need to rebind your viewmodel after your AJAX success function. You need to put a little more code before can further help you.

    Ohjay44
    • 938
    • 6
    • 19