Boky
Boky

Reputation: 12054

OnChange event function

I have the next three input's :

<input type="file" name="first"/>
<input type="file" name="second"/>
<input type="file" name="third"/>

And I want to do something if the file is chosen. I have next function :

$(function() {
    $('input[name="INPUT-NAME"]').change(function (){
       alert("Selected");
    });
});

Is there any way to pass a name of the clicked input field to this function?

Upvotes: 2

Views: 51

Answers (1)

Pranav C Balan
Pranav C Balan

Reputation: 115212

Use $(this).attr('name') method or just this.name for getting the name attribute

$(function() {
  $('input').change(function() {
    alert("Selected" + this.name);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="file" name="first" />
<input type="file" name="second" />
<input type="file" name="third" />

Upvotes: 1

Related Questions