Ash
Ash

Reputation: 169

How to copy a value from two separate HTML ids?

I've got a feeling this is an incredibly simple operation. I could just use some steer on what exactly I am doing wrong here.

I just need a simple program to take the content from <span id "text"> and copy this into <div id="text2">.

 function getValue() {
   var text = document.getElementById("text");
   document.getElementById("text2").innerHTML = text;
 };

 getValue;
<span id="text"> Hi there </span>
<div id="text2"></div>

Upvotes: 1

Views: 47

Answers (4)

hmak
hmak

Reputation: 3978

Use this code:

function getValue(){
    document.getElementById("text2").innerHTML = document.getElementById("text").innerHTML;
}

getValue();

This code should be in a onload function or in the body tag.

Upvotes: 2

Tarun Gaba
Tarun Gaba

Reputation: 1113

It seems that you are not calling function properly.

getValue();

instead of

getValue;

Upvotes: 3

sathishrtskumar
sathishrtskumar

Reputation: 19

try this jQuery code...

function getValue(){
   $("#text2").text($("#text").text());
}

this works for me...

Upvotes: -2

Joniras
Joniras

Reputation: 1336

try following:

function getValue()
{
    var text=document.getElementById("text").innerHTML;
    document.getElementById("text2").innerHTML = text;
};

Upvotes: -1

Related Questions