Good afternoon, I have a function in jQuery to know the number of characters in a given element and I needed to have the text " see more " when it was more than 300 characters and open the modal with the whole information. It turns out that jQuery I perceive a little and I do not even know where to start.
<?php
include('../includes/ligacao.php');
$sql = "SELECT * FROM objetos WHERE numero='AFS1848'";
//executa a query
$result = mysqli_query($conn,$sql);
$array=mysqli_fetch_assoc($result);
$comentarios = $array['comentarios_dados'];
print $comentarios;
?>
<html lang="en">
<head>
<title>Exemplo contagem 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.2.1/jquery.min.js"></script><scriptsrc="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<h2>Exemplo contagem modal</h2>
<table>
<tr>
<td>
<?php
echo "<p name='alvo' id='alvo'>".$comentarios."</p>";
?>
<div id="aqui"></div>
</td>
</tr>
</table>
<!-- Modal -->
<div class="modal fade" id="alvoModal" 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">Modal</h4>
</div>
<div class="modal-body">
<textarea name="alvoTextArea" rows="10" cols="30"></textarea>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Fechar</button>
</div>
</div>
</div>
</div>
</div>
<script type="text/javascript">
$(function() {
// Conta caracteres.
var tamanho = $("#alvo").text().length;
// Exibe um modal com textarea, caso o número de caracteres seja maior que 300.
if ( tamanho > 300 ) {
$("#alvoModal").modal();
}
// Mostra número de caracteres.
$("#aqui").html(tamanho);
});
</script>
</body>
</html>