Selecting text from an HTML element

Asked

Viewed 89 times

1

I’m trying to copy a text from an HTML element, it’s a code that turns the first letters of the words into uppercase (and the "da,da,dos" into lowercase) and I’m trying to implement this copy function automatically, but I’m not being able to select the text. Ignore the antics. I’m still a beginner.

<body>
    <input type="text" id="NomeMaiusculo" placeholder="Nome">
    <input type="submit" id="n importa" onclick="escola()">

*/Esse h3 aqui/*

    <h3></h3>
    <button onclick="copiarTexto()">Copiar</button>
<script>

 function escola () {

  function titleize(text) {
    var loweredText = text.toLowerCase();
    var words = loweredText.split(" ");
    for (var a = 0; a < words.length; a++) {
        var w = words[a];

        var firstLetter = w[0];


        if( w.length > 2){ 
           w = firstLetter.toUpperCase() + w.slice(1);
        } else {
           w = firstLetter + w.slice(1);
        }

        words[a] = w;
    }
    return words.join(" ");

}
var alt = document.getElementById('NomeMaiusculo');
    var alt1 = alt.value;

//selecionei ele aqui

    var h3 = document.querySelector('h3')

var b1 = (titleize(alt1));                                    
var res = b1.replace("De,Da,Dos", "de,da,dos");
  h3.textContent = (titleize(res));

  function copiarTexto () {

//quero selecionar oque tem dentro dele,depois da função titleize

            h3.select();

            document.execCommand('copy');
            }
}
  </script>
  • I even answer your question about copying a text from a phrasing element, but refactor that code because whoever answers it will be morally obliged to make considerations about the coding style.

  • What? Could you help me with that?

No answers

Browser other questions tagged

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