2

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>
1
  • 1
    var get_ad_id = $('.ad_id').val(); replace each() with val() as val gives you the value. Commented Jul 1, 2015 at 5:05

2 Answers 2

3

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('');
        }
    });
});
Sign up to request clarification or add additional context in comments.

Comments

2

Try

$(".ad_id").each(function() {
    alert($(this).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.