0

I got multiple input types in my form based on positions.

<input type="file" name="file" id="file" data-positionid="<?php echo $positionId; ?>" class="inputfile" />

Now i want on change the correct data-positionid to show. If i got multiple positions the jQuery i got responds only to the first every time.

        $('input[type=file]').change(function () {

        var positionId = $(this).data('productid');
        alert(positionId);
    });

So i think that $(this) is not realy getting the changed input type.

2 Answers 2

0

Please use the below code:

$('input[type=file]').eq(0).change(function () {

        var positionId = $(this).data('productid');
        alert(positionId);
    });

The above code will trigger element at the 0th index that is the first element.

3
  • Hi, this will get the first input type only. What i want is if i got multiple positions, so code example 1 is there multiple times, get for example input type positionId of the third input type. Commented Oct 2, 2018 at 12:16
  • Are you saying that the event is getting applied multiple times to the same element ? Commented Oct 2, 2018 at 16:50
  • Yes it is. I want for every position an image upload field. And based on the position id i know where to upload the image. Commented Oct 3, 2018 at 10:16
0

Please try below code

<input type="file" name="file" id="file" data-positionid="<?php echo $positionId; ?>" class="inputfile" onchange="getProductId(<?php echo $positionId; ?>)"/>

<script type="text/javascript">
    function getProductId(positionId) {
        alert("Value is " + positionId);
}
 </script>

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.