Pass value of a line in the table for inputs on the same page

Asked

Viewed 30 times

0

I have a simple question. I need to pass the data of a line in my table to inputs that are on the same page. Follow the screens: inserir a descrição da imagem aqui

Follows the code: inserir a descrição da imagem aqui

Does anyone have a solution? I imagine I will need javascript...?

  • Thinking quickly, would you number the <tr id="<? php echo $num ? >" > and would take the id value via jquery and pass on the <td> information, there are other ways too but this is faster

1 answer

0


Try to do it that way

$(function(){
		$('#Tabela tbody tr').on('click',function(){
			const id = $(this).attr('id');
			const cod = $('#Tabela tbody tr[id='+id+'] td').eq(0).html(),
			nome = $('#Tabela tbody tr[id='+id+'] td').eq(1).html();
			$('#frm #cod').val( cod );$('#frm #nome').val( nome );
		});
	});
<html>
	<head>
		<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" />
		<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
		<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
		<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
		<style> tr{ cursor : hand; } </style>
	</head>
<body>
	<div class="container">
		<form id="frm" name="frm" method="post">
			<label for="cod">Cód</label><input type="text" id="cod" name="cod" />
			<label for="cod">Nome</label><input type="text" id="nome" name="nome" />
		</form>
		<table id="Tabela" class="table table-sm table-hover table-light table-striped">
			<thead>
				<tr>
					<td>Cód</td>
					<td>Nome</td>
				</tr>
			</thead>
			<tbody>
				<tr id="1">
					<td>01</td>
					<td>Fulano de Tal 01</td>
				</tr>
				<tr id="2">
					<td>02</td>
					<td>Fulano de Tal 02</td>
				</tr>
			</tbody>	
		</table>
	</div>
</body>
</html>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.