I have the following code, with 3 inputs and 3 buttons. Pressing any of the 3 opens a Modal. What I want to achieve is that depending on which button I press, I transfer the value of the input to the side. Currently with my code, I always take the color "red". I understand why but my knowledge in javascript does not allow me to solve it.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Modal</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<script language="javascript">
function recibir()
{
var valor = document.getElementById("texto").value;
document.getElementById("codigo").value=valor;
}
</script>
<form id="formulario" method="Post" data-toggle="modal" data-target="#myModal">
<input type="text" id="texto" value="rojo" />
<input type="button" name="enviar" value="Enviar" onclick="recibir();"/><br>
</form>
<form id="formulario" method="Post" data-toggle="modal" data-target="#myModal">
<input type="text" id="texto" value="verde" />
<input type="button" name="enviar" value="Enviar" onclick="recibir();"/><br>
</form>
<form id="formulario" method="Post" data-toggle="modal" data-target="#myModal">
<input type="text" id="texto" value="azul" />
<input type="button" name="enviar" value="Enviar" onclick="recibir();"/><br>
</form>
<!-- Modal -->
<div class="modal" id="myModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Formulario</h4>
</div>
<div class="modal-body">
<form id="formulario" method="Post">
<label for="nombre">Nombre</label><input type="text" id="nombre"/><br>
<label for="apellido">Apellido</label><input type="text" id="apellido"/><br>
<span>Color:</span><input type="text" id="codigo"/>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</body>
</html>