Assign multiple events to several buttons

0

I am creating a grid that allows to select x number of buttons with numbers, and when the user clicks on the button it shows the number that it represents, I show the code.

$(document).ready(()=>{
	$('#item1').click(()=>{//Quisiera implementar esto en cada uno de los 
                         //botones, solo este funciona.
		$('#item1').toggleClass('active')
		$('#item1').toggleClass('disabled')	
	})
$('.item').click(()=>{
	$(this).toggleClass('active')
    $(this).toggleClass('disabled')	//Los toggleClass tampoco se ejecutan al darle click	
	let valor= $(this).attr('value')
	console.log(valor) //retorna undefined
	
})


});
<?php 
echo "
<!DOCTYPE html>
<html lang='en'>
<head>
	<meta charset='UTF-8'>
	<title>Testing</title>
	<link rel='stylesheet' href='css/main.css'>
	<!-- <script src='js/jquery.plugin.min.js'></script>	 -->
	<script src='js/jquery.min.js'></script>
	<script src='js/main.js'></script>
  <script         src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">   </script>  
</head>
<body>


	<div id='grid'>
		<table>
			";
			echo "<tr>";
				for ($i=1; $i <=500; $i++) { 
				echo "
				<td class='item active' id='item".$i."'>".str_pad($i, 3, "0", STR_PAD_LEFT)."</td>	
				";
				if ($i%10==0 || $i==0)  {
			echo "</tr><tr>";
			}
		}
		echo "	
	</table>		
</div>

</body>
</html>

";

?>

It happens that I have to add this function to each of the buttons, but it does not seem like a correct solution, to create a code for each button. I am new programming and it is one of my first tasks as a programmer, some idea of how I can solve this?

    
asked by Eleazar Ortega 12.01.2018 в 16:42
source

1 answer

1

I hope the following will work:

  $(document).ready(()=>{
        $('.clase_boton').click(()=>{
          //a cada botnton con la clase "clase_boton" se el asigan el evento click, y lo que esté aquí a dentro se va a hacer solo para el botón al cual usted le dio click
            $(this).toggleClass('active');
            $(this).toggleClass('disabled');
            let numero_boton=$(this).attr('value'); //así saca el número del botón que debe estar en el atributo "value del botón"
        })


    });
    
answered by 12.01.2018 в 16:53