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.

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.

| improve this answer | |
  • 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. – Johan SB Oct 2 '18 at 12:16
  • Are you saying that the event is getting applied multiple times to the same element ? – Sourabh Kumar Sharma Oct 2 '18 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. – Johan SB Oct 3 '18 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>
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.