Overflow text in jQuery and Bootstrap modal

0

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>
    
asked by anonymous 25.10.2017 / 16:18

2 answers

0

Well, I've developed a way for your doubt to be healed.

I did not do it in a modal because I do not think there is such a need for such implementation. What is needed for operation is in the code!

I am using a textarea to count the number of characters. However the idea of getting the text size inside the element is the same, always:

$('elemento').val().length;

$('textarea').on('keyup',function(){

  $('.qntLetras').text($(this).val().length);
  
  
  if($(this).val().length > 50)
  {
  
    $('.verMais').css('display','block');
  
  }
  else
  {
       $('.div').text($('textarea').val());
       if(  $('.verMais').css('display') == 'block')
       {
          $('.verMais').css('display','none');
       }
       
  }
  

});


$('.verMais').on('click',function(){
    var modal = document.getElementById('myModal');


var btn = document.getElementById("myBtn");


var span = document.getElementsByClassName("close")[0];

span.onclick = function() {
    modal.style.display = "none";
}


window.onclick = function(event) {
    if (event.target == modal) {
        modal.style.display = "none";
    }
}

    $('.modal-body p').text($('textarea').val());
    modal.style.display = "block";
    

});
span{

font-size: 0.8em;
color: #b3b3b3;

}


.div{

  width: 30%;
  border: 2px double coral;
  padding: 5px;

}

.verMais{
  
  display:none;
  padding: 5px;
  color: coral;

}


/* The Modal (background) */
.modal {
    display: none; /* Hidden by default */
    position: fixed; /* Stay in place */
    z-index: 1; /* Sit on top */
    padding-top: 100px; /* Location of the box */
    left: 0;
    top: 0;
    width: 100%; /* Full width */
    height: 100%; /* Full height */
    overflow: auto; /* Enable scroll if needed */
    background-color: rgb(0,0,0); /* Fallback color */
    background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
}

/* Modal Content */
.modal-content {
    position: relative;
    background-color: #fefefe;
    margin: auto;
    padding: 0;
    border: 1px solid #888;
    width: 80%;
    box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2),0 6px 20px 0 rgba(0,0,0,0.19);
    -webkit-animation-name: animatetop;
    -webkit-animation-duration: 0.4s;
    animation-name: animatetop;
    animation-duration: 0.4s
}

/* Add Animation */
@-webkit-keyframes animatetop {
    from {top:-300px; opacity:0} 
    to {top:0; opacity:1}
}

@keyframes animatetop {
    from {top:-300px; opacity:0}
    to {top:0; opacity:1}
}

/* The Close Button */
.close {
    color: white;
    float: right;
    font-size: 28px;
    font-weight: bold;
}

.close:hover,
.close:focus {
    color: #000;
    text-decoration: none;
    cursor: pointer;
}

.modal-header {
    padding: 2px 16px;
    background-color: #5cb85c;
    color: white;
}

.modal-body {padding: 2px 16px;}

.modal-footer {
    padding: 2px 16px;
    background-color: #5cb85c;
    color: white;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><textareaplaceholder='digite'></textarea><br><spanclass='qntLetras'></span><divclass='div'></div><spanclass='verMais'>VerMais</span><divid="myModal" class="modal">

  <!-- Modal content -->
  <div class="modal-content">
    <div class="modal-header">
      <span class="close">&times;</span>
      <h2>Modal Header</h2>
    </div>
    <div class="modal-body">
      <p>Some text in the Modal Body</p>
    </div>
    <div class="modal-footer">
      <h3>Modal Footer</h3>
    </div>
  </div>

</div>

I also decreased the number of characters just for testing and preserving our patience.

    
25.10.2017 / 16:48
0

Right now it's like this. But do not pass the value to the modal.

<?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>
      <style>
        span{

        font-size: 0.8em;
        color: #b3b3b3;

        }


        .div{

        width: 30%;
        border: 2px double coral;
        padding: 5px;

        }

        .verMais{

        display:none;
        padding: 5px;
        color: coral;

        }


        /* The Modal (background) */
        .modal {
            display: none; /* Hidden by default */
            position: fixed; /* Stay in place */
            z-index: 1; /* Sit on top */
            padding-top: 100px; /* Location of the box */
            left: 0;
            top: 0;
            width: 100%; /* Full width */
            height: 100%; /* Full height */
            overflow: auto; /* Enable scroll if needed */
            background-color: rgb(0,0,0); /* Fallback color */
            background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
        }

        /* Modal Content */
        .modal-content {
            position: relative;
            background-color: #fefefe;
            margin: auto;
            padding: 0;
            border: 1px solid #888;
            width: 80%;
            box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2),0 6px 20px 0 rgba(0,0,0,0.19);
            -webkit-animation-name: animatetop;
            -webkit-animation-duration: 0.4s;
            animation-name: animatetop;
            animation-duration: 0.4s
        }

        /* Add Animation */
        @-webkit-keyframes animatetop {
            from {top:-300px; opacity:0} 
            to {top:0; opacity:1}
        }

        @keyframes animatetop {
            from {top:-300px; opacity:0}
            to {top:0; opacity:1}
        }

        /* The Close Button */
        .close {
            color: white;
            float: right;
            font-size: 28px;
            font-weight: bold;
        }

        .close:hover,
        .close:focus {
            color: #000;
            text-decoration: none;
            cursor: pointer;
        }

        .modal-header {
            padding: 2px 16px;
            background-color: #5cb85c;
            color: white;
        }

        .modal-body {padding: 2px 16px;}

        .modal-footer {
            padding: 2px 16px;
            background-color: #5cb85c;
            color: white;
        }
      </style>
    </head>
    <body>
    <?php
        echo "<p name='alvo' id='alvo'>".$comentarios."</p>";
    ?>
    <br>
    <span class='qntLetras'></span>


    <div class='div'></div>
    <span class='verMais'>Ver Mais</span>

    <div id="myModal" class="modal">

      <!-- Modal content -->
      <div class="modal-content">
        <div class="modal-header">
          <span class="close">&times;</span>
          <h2>Modal Header</h2>
        </div>
        <div class="modal-body">
          <p>Some text in the Modal Body</p>
        </div>
        <div class="modal-footer">
          <h3>Modal Footer</h3>
        </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 50.
                if (    tamanho > 50   ) { 
                    $('.verMais').css('display','block');
                }
                else{
                    $('.div').text($('#alvo').val());
                    if(  $('.verMais').css('display') == 'block'){
                        $('.verMais').css('display','none');
                    }
                }
                //  Mostra número de caracteres.
                $("#aqui").html(tamanho);
            });

            $('.verMais').on('click',function(){
                var modal = document.getElementById('myModal');
                var btn = document.getElementById("myBtn");
                var span = document.getElementsByClassName("close")[0];

                span.onclick = function(){
                    modal.style.display = "none";
                }

                window.onclick = function(event){
                    if (event.target == modal) {
                        modal.style.display = "none";
                    }
                }

                $('.modal-body p').text($('#alvo').val());
                modal.style.display = "block";
            });
        </script>
    </body>
</html>
    
25.10.2017 / 18:52