0

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">
<script>
function checkValue()
    {
        var but = document.getElementById("price").value;
        console.log(but);
    }
</script>

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

4
  • 2
    id should be unique and use class for group of element Commented Nov 30, 2016 at 7:51
  • you can't have same id. Use class instead. Commented Nov 30, 2016 at 7:52
  • @PranavCBalan hmm , you mean i should pass my class attribute to my function and then i got the values? Commented Nov 30, 2016 at 7:56
  • @Wilx : check my answer Commented Nov 30, 2016 at 7:58

3 Answers 3

3

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>
<script>
  function checkValue(ele) {
    console.log(ele.value);
  }
</script>

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

6 Comments

@Wilx Although this answer does show you how to get the values for each of those buttons, this answer does not show you how to use class rather then id's to target group elements. I thought I would just point that out for future reference. this will target the element use to trigger that event and obviously .value will target the value of the element use to trigger that event which in return you get the value of the button clicked. I don't see much point posting an answer so here's an example of using a class. jsfiddle.net/dLLavug0
@NewToJs : i wanna ask what's the different about event onclick and click? i don't really get it , can you explain it for me :D
@Wilx when you use attributes it's onclick oninput onmouseover where as if you use javascript events those are click input mouseover all resulting in the same thing.
@PranavCBalan I agree, a class isn't required for something this simple but since every answer posted to this question is recommending to use a class rather than id I thought it would make sense to show an example rather than recommending it but showing a different solution.
@NewToJS : I don't think that class is what he is looking for... he just want to get the value
|
0

You should not have multiple elements with the same id. If you want to group them you can use class.

To get the value of the selected button you can send this inside the function

function checkValue(elem) {
  console.log(elem.value);
}
<button onclick="checkValue(this)" class="price" value="1">1</button>
<button onclick="checkValue(this)" class="price" value="2">2</button>
<button onclick="checkValue(this)" class="price" value="3">3</button>

Comments

0

<button onclick="checkValue(this);" class="price" id="price1" value="1">Button1
<button onclick="checkValue(this);" class="price" id="price2" value="2">Button2
<button onclick="checkValue(this);" class="price" id="price3" value="3">Button3
<script>
function checkValue(d)
    {
        var but = d.value;
        console.log(but);
    }
</script>

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.