get value of several buttons with JQUERY

1

hello guys I have a total of 10 buttons which have a value that I want to extract their value with a click depending on which is the clicked vote I'm doing with jquery but I do not know what I'm doing wrong here my code:

 
 $("#evaluate button").click(function(){

        alert($(this).val());

   })
 <div id="evaluate">
      <button value="1"><b>1</b> </button>
      <button value="2"><b>2</b> </button>
      <button value="3"><b>3</b> </button>
      <button value="4"><b>4</b> </button>
      <button value="5"><b>5</b> </button>
      <button value="6"><b>6</b> </button>
      <button value="7"><b>7</b> </button>
      <button value="8"><b>8</b> </button>
      <button value="9"><b>9</b> </button>
      <button value="10"><b>10</b> </button>
    </div>

thanks any help I thank you very much

    
asked by andy gibbs 01.07.2018 в 21:29
source

2 answers

2

I commented to you that I tested your code and it is functional about what you request, what you must keep in mind is that first the call to jquery must go and then the script with which you get the value look

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
<div id="evaluate">
      <button id="val1" value="1"><b>1</b> </button>
      <button id="val2" value="2"><b>2</b> </button>
      <button id="val3" value="3"><b>3</b> </button>
      <button id="val4" value="4"><b>4</b> </button>
      <button id="val5" value="5"><b>5</b> </button>
      <button id="val6" value="6"><b>6</b> </button>
      <button id="val7" value="7"><b>7</b> </button>
      <button id="val8" value="8"><b>8</b> </button>
      <button id="val9" value="9"><b>9</b> </button>
      <button id="val10" value="10"><b>10</b> </button>
    </div>
</body>
<script src="https://code.jquery.com/jquery-2.0.3.js"></script>

<script>
     $("#evaluate button").click(function(){

        alert($(this).val());

   })
</script>
</html>
    
answered by 01.07.2018 / 21:46
source
0

You can put a class common to all the buttons and detect which one you click directly on.

 
 $(".class-button").click(function(){

        alert($(this).val());

   })
 <div id="evaluate">
      <button class="class-button" id="val1" value="1"><b>1</b> </button>
      <button class="class-button" id="val2" value="2"><b>2</b> </button>
      <button class="class-button" id="val3" value="3"><b>3</b> </button>
      <button class="class-button" id="val4" value="4"><b>4</b> </button>
      <button class="class-button" id="val5" value="5"><b>5</b> </button>
      <button class="class-button" id="val6" value="6"><b>6</b> </button>
      <button class="class-button" id="val7" value="7"><b>7</b> </button>
      <button class="class-button" id="val8" value="8"><b>8</b> </button>
      <button class="class-button" id="val9" value="9"><b>9</b> </button>
      <button class="class-button" id="val10" value="10"><b>10</b> </button>
    </div>
    
answered by 01.07.2018 в 21:42