2

I'm trying to implement an automatic save using javascript/jQuery.

I have several (hundreds) of overlay-rotation-bounding-box-id elements. For example, overlay-rotation-bounding-box-968 would be the 968th element on my list. I want to auto save every time I change one of these elements. I'm kinda new to this so I tried to make it really simple and I used this snippet:

var autosaveOn = false;
        console.log(autosaveOn)
        function myAutosavedTextbox_onTextChanged() {
            console.log(autosaveOn);
            if (!autosaveOn) {
                autosaveOn = true;

                $('#overlay-rotation-bounding-box').everyTime("30", function () {
                    $.ajax({
                        type: "POST",
                        url: "autosavecallbackurl",
                        data: "id=1",
                        success: function (msg) {
                            $('#autosavenotify').text(msg);
                        }
                    });
                }); //closing tag
            }
        }

However, it does nothing. I guess I'm selecting the element in the wrong way. What is the correct way to develop a auto save function for an element that appears more than once and the id is based on its index?

Kind regards

vftw
  • 1,547
  • 3
  • 22
  • 51
  • Give them all the same class and use `this` inside the event handler. `$(".list-input").on("change", function() { autoSave(this); });` – freedomn-m Sep 24 '19 at 13:57
  • I can't give the same class to all of the elements since I need to select them individually. That's the tough part, I guess – vftw Sep 24 '19 at 14:03
  • It comes from the selector you're using. You don't have a selector called: overlay-rotation-bounding-box – Med.ZAIRI Sep 24 '19 at 14:08
  • Why do you need to "select them individually" - there's no evidence of that in your question. Adding a class doesn't equate to removing the id. – freedomn-m Sep 24 '19 at 14:11

1 Answers1

0

Try to change selector like this:

$('[id^=overlay-rotation-bounding-box]').everyTime.......
Himanshu Upadhyay
  • 6,558
  • 1
  • 20
  • 33