Take content from within a Div and put in Input value

Asked

Viewed 59,481 times

3

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

The input is like this:

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

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

To div in case it’s like this:

<div class="produtosIntTitulo margin-top-30">Produto teste 1</div>

5 answers

6


I did with Jquery for you to take a look at how to pick up the text from div

Jquery

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

You can follow the result in this DEMO

  • It worked 100%. Thank you.

  • In my case I need to log in to another page and pick up the contents of a div. You know how I could log in?

  • I need more details to give you the answer @Devidyoliviera

  • @Diegovieira I need a javascript/jQuery function that will login to another site and pick up the contents of a DIV. I have this question here that explains better (http://answall.com/questions/54703/fazer-login-em-um-site-via-jquery-javascript?noredirect=1#comment112426_54703)

1

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

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

Example: http://jsfiddle.net/ronnyamarante/V4ebK/

1

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>

Making a javascript function for this, you could call it to in some event of your input (in the 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>
  • Could do using document.querySelector ECMA5 to not pollute with ID and maintain its classes :)

1

With javascript pure, without the use of jQuery would be so:

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

Online demo.

0

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

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

Browser other questions tagged

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