0

I'm attempting to clear all inputs with the class "new" on blur, but for some reason it just won't work. I've bashed my head at the this for three hours now, which obvious point am I missing? Relevant code below.

UPDATE 2

I tried changing out the switch-case block with corresponding if blocks, and they give the expected result. This eliminates the current problem, but I don't find it to be a viable answer to the original question which is why my origianl code with switch-case doesn't work.

UPDATE 1

After some research and experimenting I've discovered that I can clear all inputs with the class "new" as long as they're not inside my switch-case block. The selector I'm testing with is $('.new'), once inside the switch-case block this gives no visible effect.

@{
    ViewBag.Title = "Viser infrastruktur";
    Layout = "~/Views/Shared/_SuperOfficeLayout.cshtml";
}

<table class="table table-striped compact hover row-border">
    <thead>
        <tr>
            <th>Produsent</th>
            <th>Modell</th>
            <th>Serienummer</th>
            <th>Firmware</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>
                <div class="control-group">
                    <input type="text" class="col-md-12 form-control autosave new" name="manufacturer" placeholder="Produsent" value="" />
                    <input type="hidden" class="autosave new" name="id" value="" />
                    <input type="hidden" class="autosave new" name="superOfficeCustomerId" value="@Model.SuperOfficeCustomerId" />
                </div>
            </td>
            <td>
                <div class="control-group">
                    <input type="text" class="col-md-12 form-control autosave new" name="model" placeholder="Modell" />
                </div>
            </td>
            <td>
                <div class="control-group">
                    <input type="text" class="col-md-12 form-control autosave new" name="serialNumber" placeholder="Serienummer" />
                </div>
            </td>
            <td>
                <div class="control-group">
                    <input type="text" class="col-md-12 form-control autosave new" name="firmware" placeholder="Firmware" />
                </div>
            </td>
        </tr>
        @foreach (var infrastructure in Model.Infrastructures)
        {
            <tr>
                <td>
                    <div class="control-group">
                        <input type="text" class="col-md-12 form-control autosave" name="manufacturer" placeholder="Produsent" value="@infrastructure.Manufacturer" />
                        <input type="hidden" class="autosave" name="id" value="@infrastructure.Id" />
                        <input type="hidden" class="autosave" name="superOfficeCustomerId" value="@Model.SuperOfficeCustomerId" />
                    </div>
                </td>
                <td>
                    <div class="control-group">
                        <input type="text" class="col-md-12 form-control autosave" name="model" placeholder="Modell" value="@infrastructure.Model" />
                    </div>
                </td>
                <td>
                    <div class="control-group">
                        <input type="text" class="col-md-12 form-control autosave" name="serialNumber" placeholder="Serienummer" value="@infrastructure.SerialNumber" />
                    </div>
                </td>
                <td>
                    <div class="control-group">
                        <input type="text" class="col-md-12 form-control autosave" name="firmware" placeholder="Firmware" value="@infrastructure.Firmware" />
                </div>
            </td>
        </tr>
    }
</tbody>

@section SpecializedScripts
{
    <script type="text/javascript">
        function saveSettings(element, ajaxUrl, ajaxType) {
            var fields = $(element).closest('tr').children('td').children('div').children('.autosave');
            var abort = false;

            var ajaxData = {};
            $(fields).each(function () {
                abort = ($(this).val() == '' || $(this).val() == null);
                backgroundColor = abort == true ? '#d9534f' : '#f9f598';

                $(this).css('background-color', backgroundColor).css('color', '#ffffff').stop().animate({ 'background-color': '#ffffff', 'color': '#000000' }, 1500);

                ajaxData[$(this).prop('name')] = $(this).val();
            });

            if (abort == true) {
                return false;
            }

            $.ajax({
                url: ajaxUrl,
                type: ajaxType,
                data: ajaxData
            }).success(function (data, textStatus, jqXHR) {
                $(fields).each(function() {
                    $(this).css('background-color', '#5cb85c').css('color', '#ffffff').stop().animate({ 'background-color': '#ffffff', 'color': '#000000' }, 1500);
                });

                switch(data.status)
                {
                    case 'Deleted':
                        $(element).closest('tr').remove();
                    break;
                    case 'Added':
                        var tableBody = $('tbody');
                        var html = '<tr>';
                        for (var field in data.result) {
                            if (field == 'id' || field == 'superOfficeCustomerId')
                            {
                                html += '<input type="hidden" class="autosave" name="' + field + '" value="' + data.result[field] + '" />';
                            }
                            else {
                                html += '<td>'
                                        + '<div class="control-group">'
                                            + '<input type="text" class="col-md-12 autosave form-control" name="' + field + '" value="' + data.result[field] + '" />'
                                        + '</div>'
                                    + '</td>';

                                $('input.new[name=' + field + ']').val('');
                            }
                        }
                        html += '</tr>';
                        $(tableBody).append(html);
                    case 'Modified':
                        $(fields).each(function () {
                            $(this).val(data.result[$(this).prop('name')]);
                        });
                    break;
                }
            }).fail(function () {
            });
        }

        $(document).on('blur', 'input.autosave', function () {
            saveSettings($(this), '/Link/SaveInfrastructure', 'POST');
        });

        $(document).on('change', 'input.new', function () {
        });
    </script>
}
Maritim
  • 2,111
  • 4
  • 29
  • 59

2 Answers2

0

Something like this should work:

$('input.new').on('blur', function() { $(this).val(''); $(this).text(''); });

just make sure the input exists when you bind the event otherwise you can do:

$(document).on('blur', 'input.new', function() { $(this).val(''); $(this).text(''); });

Zach Leighton
  • 1,939
  • 14
  • 24
  • This isn't very different from what I already have. Why would you run text('') after val('')? val('') should be sufficient, but it appears not to be in this case. – Maritim Oct 22 '14 at 12:49
  • Depends on the input type, I've also had some browsers clear the value but the text in the box wasn't changed right away. – Zach Leighton Oct 22 '14 at 12:50
  • I see. Well, I added text('') under my val('') call, still doesn't show it cleared in the browser, but if I attempt to console log the value of the field after the call to val('') it shows the value as empty. Doesn't show in browser though. Any ideas? – Maritim Oct 22 '14 at 12:51
  • Can you make a fiddle of this? Then we can try and debug it. – Zach Leighton Oct 22 '14 at 12:52
  • Also I'd check the order of the bindings, if the other binding on autosave is trapping blur it may never fire off the other event handler. – Zach Leighton Oct 22 '14 at 12:58
  • How can it trap blur? – Maritim Oct 22 '14 at 13:04
  • If this is happening somewhere http://stackoverflow.com/questions/6157486/jquery-trap-all-click-events-before-they-happen – Zach Leighton Oct 22 '14 at 13:13
0

For vanilla JavaScript, you can use

<input onBlur={(event) => { event.target.value = ''; }} />
Michael Yurin
  • 1,021
  • 14
  • 18