0

My html and jquery code looks like below but for some reason I can change the text of the span. I m trying with both find and closest methods. Any ideas? thanks

<body>
<span class="classA">Text A</span>
<input id="testId" type="file" name="files" class="fileUp">
<span class="classA">Text B</span>

<script type="text/javascript">

    $(document).ready(function () {
        $("input.fileUp").change(function (event) {
            $(this).closest('span.classA').text('testing');
            $(this).find('span.classA').text('testing');

        });
    });
</script>
</body>
1
  • use: $(this).parent().find('span.classA').text('testing'); Commented Mar 20, 2016 at 18:03

1 Answer 1

1

span.classA is not the closest element of .fileUp. Use prev() method like following.

$("input.fileUp").change(function (event) {
    $(this).prev('span.classA').text('testing');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span class="classA">Text A</span>
<input id="testId" type="file" name="files" class="fileUp">
<span class="classA">Text B</span>

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

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.