bryan
bryan

Reputation: 9369

Find what specific part of the form has changed on input

I have multiple forms on my page with the following function:

$('#pageContainer').on('input propertychange change', '#form1', function() {}

Within this function, I make 11 function calls that formats text for 11 different textarea box's. I don't need to call all 11, just the one's that have changed that need formatting.

Is there a way to figure out what part of the form has been changed that made the function call so that I can call the correct (1 of 11) functions, or none at all?

So basically, if textarea 1-11 has been the input that calls the .on(), call that specific function. If not, don't call anything.

Upvotes: 0

Views: 47

Answers (2)

Matt Fellows
Matt Fellows

Reputation: 6522

I don't think there's a nice handy function to check these things, but you could assign an event handler on the onChange event, this could add $(this).attr("id") to an array. You could then construct a selector based on that array?

for example

var changes = [];
$("input").on('change', function() {
    changes.push($(this).attr(id));
});

function yourFormattingFunction() {
    var selector = "#" + changes.splice(", #");
    $(selector).each(function() {
        //...Do your formatting here
    });
}

Obvious improvements like making sure the list is unique etc can be done to improve this...

Upvotes: 0

PSL
PSL

Reputation: 123739

You can use event.target to find out which element caused the change event.

   $('#pageContainer').on('input propertychange change', '#form1', function(e) {
       var elementId = e.target.id;
       //Do you actions based on this
   }

Upvotes: 3

Related Questions