Get content from within a Div and put in the value of Input


I have a form, and inside it I have an input, which will have to get what it contains inside a DIV, which in this case is just text.

The input looks like this:

<input class='formContato' type='text' name='orcamentoAssuntoForm' id='orcamentoAssuntoForm' value='Assunto'/>

In this case, the contents of this div must be within value .

The div in case is thus:

<div class="produtosIntTitulo margin-top-30">Produto teste 1</div>
asked by anonymous 14.07.2014 / 14:39

5 answers


I made it with Jquery so you can take a look at how to get the text from div


    var valorDaDiv = $(".produtosIntTitulo").text();    

You can track the result at DEMO

14.07.2014 / 14:49

In the case of the form you will use val() to make the set and in the div it will use text() to perform the get.

 $( '.formContato' ).val($( '.produtosIntTitulo' ).text());

Example: link

14.07.2014 / 14:49

With javascript pure, without using jQuery would look like this:

var produtosIntTitulo = document.getElementsByClassName("produtosIntTitulo");
var orcamentoAssuntoForm = document.getElementById("orcamentoAssuntoForm");
orcamentoAssuntoForm.value = produtosIntTitulo[0].innerText;

Demo online

14.07.2014 / 14:58

You can capture and change the value of a div using getElementById() .

          <script type="text/javascript">
              function alteraDiv(){
                    var destino = document.getElementById("minhadiv");
                    destino.value = document.getElementById("orcamentoAssuntoForm").value;;

By doing a javascript function for this, you could call it for some instance of your input (on onchange for example)

         <input class='formContato' type='text' name='orcamentoAssuntoForm' id='orcamentoAssuntoForm' value='Assunto' onchange="alteraDiv()"/>

         <div id="minhadiv" class="produtosIntTitulo margin-top-30">Produto teste 1</div>
14.07.2014 / 14:55

You can use pure JavaScript only to do this if you prefer ...

   window.onload = function(){
        var divconteudo = document.getElementsByClassName("produtosIntTitulo margin-top-30")[0].textContent;
        document.getElementById("orcamentoAssuntoForm").value = divconteudo;
14.07.2014 / 15:00