2

So my code is something like this:-

for($i=0;$i<$length;$i++){
?>
<div id="scoreDiv">
    <input type="text"  name="myscore"  placeholder="Enter your score" required>
    <input type="text"  name="oppoScore"  placeholder="Your opponent score" required>
    <input id="submitScore" type="submit" value="submit" >
</div>
<?php
}
?>

In Jquery I want to detect which button was clicked and based on that button I want to take the input field entered by user.

4
  • 1
    use this context with class Commented Feb 9, 2017 at 7:16
  • id of submit need to convert to class and then use jquery this Commented Feb 9, 2017 at 7:16
  • 1
    It worked.Thanks a lot Commented Feb 9, 2017 at 7:22
  • 1
    Add the same class to all buttons, create onclick event for that class that reads this id. Commented Feb 9, 2017 at 7:29

3 Answers 3

2

You can do it like below (one example):-

$(document).ready(function(){
  $('.submitScore').click(function(e){
       e.preventDefault();
      alert($(this).prev().attr('name'));
    
    });
  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="scoreDiv">

                <input type="text"  name="myscore"  placeholder="Enter your score" required>
                <input type="text"  name="oppoScore"  placeholder="Your opponent score" required>
                <input class="submitScore" type="submit" value="submit" >

        </div>
<div id="scoreDiv">

                <input type="text"  name="myscore1"  placeholder="Enter your score1" required>
                <input type="text"  name="oppoScore1"  placeholder="Your opponent score1" required>
                <input class="submitScore" type="submit" value="submit" >

        </div>

Note:- class is treated as group-selector while id is treated as individual-selector in Jquery.Thanks

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

Comments

1

You could try something like the below:

$(function(){

    $(".js-btn").on("click",function(){
        var btn = $(this);
        var txtBoxes = btn.siblings('input');
        console.log($(txtBoxes[0]).val());
        console.log($(txtBoxes[1]).val());
    });
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="scoreDiv">
    <input type="text"  name="myscore"  placeholder="Enter your score" required>
    <input type="text"  name="oppoScore"  placeholder="Your opponent score" required>
    <input id="submitScore" type="submit" value="submit" class="js-btn" >
</div>
<div id="scoreDiv">
    <input type="text"  name="myscore"  placeholder="Enter your score" required>
    <input type="text"  name="oppoScore"  placeholder="Your opponent score" required>
    <input id="submitScore" type="submit" value="submit" class="js-btn" >
</div>
<div id="scoreDiv">
    <input type="text"  name="myscore"  placeholder="Enter your score" required>
    <input type="text"  name="oppoScore"  placeholder="Your opponent score" required>
    <input id="submitScore" type="submit" value="submit" class="js-btn" >
</div>
<div id="scoreDiv">
    <input type="text"  name="myscore"  placeholder="Enter your score" required>
    <input type="text"  name="oppoScore"  placeholder="Your opponent score" required>
    <input id="submitScore" type="submit" value="submit" class="js-btn" >
</div>

Comments

0

Since id should be unique, Use class attribute instead of id and try below code:

<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script type="application/javascript">
    $(document).ready(function () {
        $('.submitScore').on('click',function () {
            var div_el = $(this).parent('.scoreDiv');
            var myscore = div_el.find('input[name="myscore"]').val();
            var oppoScore = div_el.find('input[name="oppoScore"]').val();

            alert(myscore+'----'+oppoScore);
            return false;
        })
    });
</script>
</head>





<?php
for($i=0;$i<5;$i++){
?>
<div class="scoreDiv">

<input type="text"  name="myscore"  placeholder="Enter your score" required>
<input type="text"  name="oppoScore"  placeholder="Your opponent score" required>
<input class="submitScore" type="submit" value="submit" >

</div>
<?php
}
?>

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.