1

i need to get some value from one hidden id and pass it to another ID as inner HTML.

here my code

<script>
function replaceText(){
var x=document.getElementById("mainTitle1").value;
document.getElementById("mainTitle").innerHTML=x;
}
</script>

Here HTML File

<span id="mainTitle"></span>

<span style="display:none;" id="mainTitle1">Text Content</span>

but this isn't working. I'm getting 'undefined'

1
  • 2
    span elements don’t have a value. Commented Aug 21, 2014 at 9:02

3 Answers 3

1
<script>
function replaceText(){
var x=document.getElementById("mainTitle1").innerHTML;   //Correct here 
document.getElementById("mainTitle").innerHTML=x;
}
</script>

The Problem is with .value change it to .innerHTML

Sign up to request clarification or add additional context in comments.

5 Comments

GetElementById what is this?
fetching the tag with the particular id from the HTML
@Girish Sorry my mistake.. fixed it. but still getting 'undefined'
@ChathuraLakmal..try using innerhtml instead of value
@ChathuraLakmal value is working with form elements.. you should use there innerHTML or textContent
0

GetElementById should be getElementById JavaScript is case sensitive language

2 Comments

I'm getting Undefined in this field <span id="mainTitle"></span>
@ChathuraLakmal value is working with form elements.. you should use there innerHTML or textContent
0

easy :

var x=document.getElementById("mainTitle1").innerHTML;
document.getElementById("mainTitle").innerHTML=x;

Working DEMO

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.