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

3

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

4

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

Jquery

$(function(){
    var valorDaDiv = $(".produtosIntTitulo").text();    
    $("#orcamentoAssuntoForm").val(valorDaDiv);
});

You can track the result at DEMO

    
14.07.2014 / 14:49
1

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.

$(document).ready(function(){
 $( '.formContato' ).val($( '.produtosIntTitulo' ).text());
});

Example: link

    
14.07.2014 / 14:49
1

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
0

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;;
              }
        </script>

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
0

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