0

I have 3 input types. One textbox, two dates. I am setting up a custom attribute itself in the HTML as data-ischanged = false. In the change function, I am making it true. After saving the data (ajax call). I am trying to change the custom property to false once again. But I am unable to do so.

Any help will be appreciated.

HTML

                    <label>Description</label>
                    <input type="text" id="SelectedLookupTypeDescription" data-ischanged="false" />

                    <label style="padding:12px 12px">Eff. Start Date</label>
                    <input type="date" id="SelectedStartDate" data-ischanged="false" />

                    <label style="padding:12px 12px">Eff. End Date</label>
                    <input type="date" id="SelectedEndDate" data-ischanged="false" />

OnChange function below :

$('#SelectedLookupTypeDescription').on('change', function () {
    $('#SelectedLookupTypeDescription').attr("data-ischanged", true);
});

$("#SelectedStartDate").on('change', function () {
    $("#SelectedStartDate").attr("data-ischanged", true);
});

$('#SelectedEndDate').on('change', function () {
    $("#SelectedEndDate").attr("data-ischanged", true);
});

After the successful ajax call, using the below code I am trying to set the value to false

          var descselector = document.getElementById('SelectedLookupTypeDescription');
                descselector.setAttribute("data-ischanged", false);

                var StartSelector = document.getElementById('SelectedStartDate');
            StartSelector.setAttribute("data-ischanged", false);

                var EndSelector = document.getElementById('SelectedEndDate');
                EndSelector.setAttribute("data-ischanged", false);`

AJAX Code

    $.ajax({
        type: 'POST',
        url: '/Reference/UpdateLookupTypeData',
        data: { TypeData: TypeData },
        beforeSend: function (xhr) { xhr.setRequestHeader('X-XSRF-TOKEN', csrfToken); },
        success: function (RefResponse) {
            toastr.success("Data saved successfully");
            getOtherDetails(Id);

                //var descselector = document.getElementById('SelectedLookupTypeDescription');
                //descselector.setAttribute("data-ischanged", false);

                var StartSelector = document.getElementById('SelectedStartDate');
            StartSelector.setAttribute("data-ischanged", false);

                var EndSelector = document.getElementById('SelectedEndDate');
            EndSelector.setAttribute("data-ischanged", false);
           }
        });
freedomn-m
  • 27,664
  • 8
  • 35
  • 57
ispostback
  • 330
  • 2
  • 7
  • 23
  • Why not use `$('#SelectedLookupTypeDescription').attr("data-ischanged", false);` in the latter code? – David May 11 '21 at 11:24
  • can you show us your ajax code, because this code looks fine – Abhishek Pandey May 11 '21 at 11:25
  • @David, i tried that way.but it is not working – ispostback May 11 '21 at 11:29
  • How are you checking if they've changed? I'm assuming you're *not* using `$("#SelectedEndDate").data("ischanged");` ? – freedomn-m May 11 '21 at 11:34
  • @freedomn-m, i am using that – ispostback May 11 '21 at 11:35
  • Out of interest, why do you use `$("#SelectedEndDate").attr("data-ischanged", true);` then `document.getElementById('SelectedStartDate').setAttribute("data-ischanged", false);` why not use the same method on both:? – freedomn-m May 11 '21 at 11:35
  • 1
    Ah, well there's your issue `.data()` only reads the attr once - then it caches the result. If you're using `.data()` then *always* use `.data()` - there's multiple answers that explain it better. – freedomn-m May 11 '21 at 11:36
  • @because, after the change , i need to save the data. After save, i have to moify the property. Can you provie any code sample over here of your suggestion – ispostback May 11 '21 at 11:37
  • 1
    No, I mean you set `true` with `.attr("data-ischanged", true);` why not set `false` with `.attr("data-ischanged", false);`? (won't work if you're reading with `.data` but seemed odd to use jquery for true but vanilla for false. – freedomn-m May 11 '21 at 11:40
  • 1
    I've seen better answers that explain it, but here's one: https://stackoverflow.com/a/30543817/2181514 – freedomn-m May 11 '21 at 11:40
  • I got the answer from the previous one. Thanks for your help – ispostback May 11 '21 at 11:49
  • @freedomn-m, if you wrtite it down as an answer. I will accept it – ispostback May 11 '21 at 11:50

1 Answers1

-1

Try the values to "false" not false

jsayev
  • 1
  • 1