Wilx Wilx - 1 year ago 62
HTML Question

How to check some button value with same id javascript

well guys , i'm new in here ...
how can i check the values on different button with the same id?

sample code:

<button onclick="checkValue();" id="price" value="1">
<button onclick="checkValue();" id="price" value="2">
<button onclick="checkValue();" id="price" value="3">
function checkValue()
var but = document.getElementById("price").value;

well .. how can i obtain 1 or 2 or 3 when it was clicked?

Answer Source

The id should be unique so always use class for the group of elements. For getting the value pass the this context as the argument.

<button onclick="checkValue(this);" value="1">1</button>
<button onclick="checkValue(this);" value="2">2</button>
<button onclick="checkValue(this);" value="3">3</button>
  function checkValue(ele) {

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download