Search code examples
javascriptjqueryinternet-explorer-9onblur

.blur() does not trigger when clicking outside datepicker


I have a .blur() event for four text inputs, that has datepicker and timepicker attached to them. When i "tab" to the next control, it works fine, but when i click somewhere outside, the event does not trigger. Am I doing something wrong, or is there a problem with the .blur() event?

JavasSript code for the event:

    $(".calcTimespan").blur(function () {
        $.ajax({
            type: "GET",
            url: '@Url.Action("CalculateTimespan")',
            data: {
                "franDatum": $("#Dyk_FranDatum").val(),
                "franTid": $("#Dyk_FranTid").val(),
                "tillDatum": $("#Dyk_TillDatum").val(),
                "tillTid": $("#Dyk_TillTid").val()
            },
            dataType: 'html',
            cache: false,
            success: function (data) {
                if (data != "FAIL")
                    $("#Dyk_DyktidMinuter").val(data);
            },
            error: function (jqXHR, textStatus, errorThrown) {
                alert("Ett fel inträffade vid hämtning av dykning!");
            }
        });
    });

The HTML:

    <div>
        <label for="Fr_n_datum">Fr&#229;n datum</label>
        <label style="margin-left: 40px;">Tid</label>
        <br />
        <input class="halv datumpicker calcTimespan" id="Dyk_FranDatum" name="Dyk.FranDatum" type="text" value="" />
        <input class="halv tidpicker calcTimespan" id="Dyk_FranTid" name="Dyk.FranTid" type="text" value="" />
    </div>
    <br />

    <div>
        <label for="Till_datum">Till datum</label>
        <label style="margin-left: 50px;">Tid</label>
        <br />
        <input class="halv datumpicker calcTimespan" id="Dyk_TillDatum" name="Dyk.TillDatum" type="text" value="" />
        <input class="halv tidpicker calcTimespan" id="Dyk_TillTid" name="Dyk.TillTid" type="text" value="" />
    </div>
    <br />

    <div>
        <label for="Dyktim__min_">Dyktim (min)</label>
        <label style="margin-left: 33px;">Pris per minut</label>
        <br />
        <input class="halv" id="Dyk_DyktidMinuter" name="Dyk_DyktidMinuter" type="text" value="" />
        <input class="halv" id="Dyk_Pris" name="Dyk.Pris" type="text" value="" />
    </div>

I'm targeting IE9 only.

EDIT It seems that the events are working fine. The problem is that when I'm clicking in the datepicker to select a date, already at the mouse-down on the date, the event is triggered, even though the value is not already set by the component in the input-text!

Now, I have to find a way to either get the "mouse down"-date or delay the event to when it's actually set in the input-text.


Solution

  • The issue I was actually having was this: jqueryUI datepicker fires input's blur before passing date, avoid/workaround?

    I used a variant of Purells answer to solve my problem.

    Thanks for all support. It was the help in this question that led me to the real problem.