0

I have several dynamic forms on a page that each have a file input... How do I do I target the correct file input using $(this) in javascript?

Here is my form

<form enctype="multipart/form-data" action="category_manage.php" method="post">
<div class="plus-button-container">
    <input id="upfile" name="photo" type="file" onchange="submitFormAfterImageCheck();"/>
</div>
<input type="hidden" name="sml_image" value="sml_image" />
</form>

And here is my javascript function

function submitFormAfterImageCheck() {
    var formSubmit = $(this).closest("form");
    var file = $(this).closest('input[type=file]').val();
    alert(file);
}
//gives me undefined

I also tried this that works, but only for the first form ...

function submitFormAfterImageCheck() {
    var formSubmit = $(this).closest("form");
    var file = $('input[type=file]').val();
    alert(file);
}

I think I need something like this, but this gives me undefined

var file = $(this).find('input[type=file]').val();
2
  • 1
    You are using jQuery, use jQuery to bind the events, inline events are bad practice. Commented Mar 6, 2014 at 0:03
  • just use the form that you resolved as the context for the file selector: var file = $('input[type=file]', formSubmit).val(); Commented Mar 6, 2014 at 0:10

3 Answers 3

2

Since you have an inlined event handler, inside the event handler this does not refer to the changed element.

One easy solution is to pass the changed element as an parameter to the event handler like

<input id="upfile" name="photo" type="file" onchange="submitFormAfterImageCheck(this);"/>

then

function submitFormAfterImageCheck(el) {
    var formSubmit = $(el).closest("form");
    var file = $(el).val();
    alert(file);
}

Note: Since you are using jQuery it is better to use jQuery based event handlers instead of inlined ones

Sign up to request clarification or add additional context in comments.

Comments

0

The way you have it submitFormAfterImageCheck is executing with the global object(window) as this.
I'd suggest separating code logic from the markup with jQuery event handlers.

<form enctype="multipart/form-data" action="category_manage.php" method="post">
<div class="plus-button-container">
    <input id="upfile" name="photo" type="file" onchange="submitFormAfterImageCheck();"/>
</div>
<input type="hidden" name="sml_image" value="sml_image" />
</form>
<script>
function submitFormAfterImageCheck() {
    var formSubmit = $(this).closest("form");
    var file = $(this).val();
    alert(file);
}
$('input[type="file"]').on('change', submitFormAfterImageCheck);
</script>

Comments

0
<input id="upfile" name="photo" type="file" onchange="submitFormAfterImageCheck(this);"/>

and :

function submitFormAfterImageCheck(source) { 
    alert($(source).val());
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.