I have a problem to get particular textbox value inside a loop using jquery. please see the below code. I will get 1 to 10 values all the textbox next to print the button. when I submit that particular textbox value, it will always coming that first value as "1".
I know in jquet code I have to put "$this" key word to get that particular value, but I am new to jquery and I tried so many methods to do that, I failed. please help.
<?PHP for($i=0, $i<10; $i++) ?>
<input type="text" name="ad_id" value="<?PHP echo $i; ?>" class="ad_id" />
<button type="button" class="ad_increase">Submit</button>
<?PHP } ?>
<script>
$('.ad_increase').click(function() {
var get_ad_id = $('.ad_id').each();
$.post('includes/show_ad_id.php', {
ad_id: get_ad_id
}, function(res) {
if (res != 1) {
$('.display_in_model').html(res);
} else {
$('.display_in_model').html('');
}
});
});
</script>
Try
$(".ad_id").each(function() {
alert($(this).val());
});
Use prev()
to get the value of previous input
. Also, change each()
to val()
to get the value of the input.
var get_ad_id = $(this).prev('.ad_id').val();
Code
$('.ad_increase').on('click', function() {
var get_ad_id = $(this).prev('.ad_id').val();
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
$.post('includes/show_ad_id.php', {
ad_id: get_ad_id
}, function(res) {
if (res != 1) {
$('.display_in_model').html(res);
} else {
$('.display_in_model').html('');
}
});
});