1

JQUERY

When I clicked the both 2 buttons it only returns the value 1

$(document).ready(function() {
    var getvalue = $(".view_btn").val();
    $(".view_btn").click(function() {
        alert(getvalue);
    });
});

PHP

<?php foreach ($studentRankingViewGET as $studentRankingViewSHOW) {?>
<input type="button" value="<?php echo $studentRankingViewSHOW['id'];?>" class="view_btn">
<?php } ?>

This returned 2 values .i.e. 1 AND 2

2
  • Share relevant code i.e. where is .btn element? and also make it readable. .val() will always return value of first element from matched elements. So either iterate them using .each() or use DOM relationship to target them Commented Jun 4, 2018 at 8:28
  • There is no AJAX code given.... so, what exactly is the question? Commented Jun 4, 2018 at 8:55

3 Answers 3

2

$('.view_btn').click(function(){
alert($(this).val());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<input type="button" value="123" class="view_btn"/>
   <input type="button" value="456" class="view_btn"/>

you should use $(this) as referring object.

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

Comments

0

Since you have two buttons with same class. You can get the clicked element with this.

$(".view_btn").click(function() {
     alert($(this).val());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="view_btn" value="Button 1">Button 1<button>
<button class="view_btn" value="Button 2">Button 2<button>

Comments

0

This will support for dynamic content also

$(".view_btn").on("click", function() {
     alert($(this).val());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="view_btn" value="Button 1">Button 1<button>
<button class="view_btn" value="Button 2">Button 2<button>

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.