0

When I press a button I get the value of the 1st button. I want the value of the button that was pressed.

JavaScript

function GetHadess() {
        var title = $(".myButtonn").val();
        alert(title);    
}

The value of the button that was pressed should be shown in alert, but here value of the 1st button is shown.

HTML code

<tbody id="tbl">
<tr><td><input class="myButtonn" type="submit" name="title1" value=" bbb " onclick="GetHadess()"></td></tr>
<tr><td><input class="myButtonn" type="submit" name="title1" value=" bb " onclick="GetHadess()"></td></tr>
</tbody>
1
  • pass this to the function and use this as context to get the value. or create an event hanadler Commented Sep 15, 2017 at 7:28

4 Answers 4

3

You have to pass this context to the function so that it will know which button clicked,like below:-

function GetHadess(element) {
  var title = $(element).val();
  alert(title);    
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<tbody id="tbl">
  <tr>
    <td><input class="myButtonn" type="submit" name="title1" value=" bbb " onclick="GetHadess(this)">
    </td>
  </tr>
  <tr>
    <td><input class="myButtonn" type="submit" name="title1" value=" bb " onclick="GetHadess(this)">
    </td>
  </tr>
</tbody>

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

Comments

2
  1. Use this context to tell which is the click button

$('.myButtonn').click(function() {

  alert($(this).val())
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tbody id="tbl">
    <tr>
      <td><input class="myButtonn" type="submit" name="title1" value=" bbb " onclick="GetHadess()"></td>
    </tr>
    <tr>
      <td><input class="myButtonn" type="submit" name="title1" value=" bb " onclick="GetHadess()"></td>
    </tr>
  </tbody>
</table>

Comments

0

Change your code like this:

<tbody id="tbl">
<tr><td><input class="myButtonn" type="submit" name="title1" value=" bbb " onclick="GetHadess(this)"></td></tr>
<tr><td><input class="myButtonn" type="submit" name="title1" value=" bb " onclick="GetHadess(this)"></td></tr>
</tbody>


function GetHadess(this) {
        var title = this.val();
        alert(title);    
}

Comments

0

Instead of getting value using class, Pass event in the function and access value using event.target.value.

function GetHadess(e) {
        console.log(e.target.value);
        alert(e.target.value);    
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<tbody id="tbl">
<tr><td><input class="myButtonn" type="submit" name="title1" value=" bbb " onclick="GetHadess(event)"></td></tr>
<tr><td><input class="myButtonn" type="submit" name="title1" value=" bb " onclick="GetHadess(event)"></td></tr>
</tbody>

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.