Input text value into a div?

Say I have this text box:

<input type="text" id="myText" placeholder="Enter Name Here">

Upon pressing a button, I would like to send the value entered into this div:

<div id="text2"></div>

I'm not entirely sure how to do this. Do I create a function and call it to the div? How would I do that?

Could someone clear this up for me? Thanks.



Add an onclick to your button:

<input type="button" id="somebutton" onclick="addText()">

Then write the javascript:

function addText()
    document.getElementById('text2').innerHTML = document.getElementById('myText').value;

Solution using onclick event:

<input type="text" id="myText" placeholder="Enter Name Here">
<div id="text2"></div>
<button id="copyName" onclick="document.querySelector('#text2').innerHTML = document.querySelector('#myText').value" value="Copy Name"></button>



You can manipulate the content inside the div from javascript code. Your button should trigger a function (using the onclick event), which would access the specific div within the DOM (using the getElementById function) and change its contents.

Basically, you'd want to do the following:

<!DOCTYPE html>
function changeContent() {
    document.getElementById("myDiv").innerHTML = "Hi there!";

<div id="myDiv"></div>
<button type="button" onclick="changeContent()">click me</button>


Mark D, You need to include javascript to handle the button click, and in the function that the button calls, you should send the value into the div. You can call $("#myText").val() to get the text of the text box, and $("#txtDiv").text(txtToAppend) to append it to the div. Please look at the following code snippet for an example.

function submitTxt() {
<script src=""></script>
<input type="text" id="myText" placeholder="Enter Name Here">
<button onclick = "submitTxt()"> Submit </button>
<div id="txtDiv"> </div>


HTML could be:

<input type='text' id='myText' placeholder='Enter Name Here' />
<input type='button' id='btn' value='click here' />
<div id='text2'></div>

JavaScript should be external:

var pre = onload; // previous onload? - window can only have one onload property using this style of Event delegation
onload = function(){

var doc = document, bod = doc.body;
function E(e){
  return doc.getElementById(e);
var text2 = E('text2'); // example of Element stored in variable
E('btn').onclick = function(){
  text2.innerHTML = E('myText').value;


I would recommend using a library like jQuery to do this. It would simplify the event handling and dom manipulation. None the less, I will include vanilla JS and jQuery examples.

Assuming the HTML in the body looks like this:

    <input id="myText" type="text" placeholder="Enter Name Here">
    <input type="submit" id="myButton">
<div id="text2"></div>

The Vanilla JS example:

//Get reference to button
var myButton = document.getElementById('myButton');
//listen for click event and handle click with callback
myButton.addEventListener('click', function(e){
    e.preventDefault(); //stop page request
    //grab div and input reference
    var myText = document.getElementById("myText");
    var myDiv = document.getElementById("text2");
    //set div with input text
    myDiv.innerHTML = myText.value;

When possible avoid using inline onclick property, this can make your code more manageable in the long run.

This is the jQuery Version:

//Handles button click
    e.preventDefault(); //stop page request
    var myText = $('#myText').val(); //gets input value
    $('#text2').html(myText); //sets div to input value

The jQuery example assumes that you have/are adding the library in a script tag.


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.