0

I want then to fill in a date in the inputfield "datum" and that date have to replace the content of the div "test1". Can anyone help me out :P? I'm struggling on this for a while now haha. This is my code :

<form>
    Datum: <input type="text" id="datum">
</form> 

<script>

    var MyDiv1 = Document.getElementById('datum');

    document.getElementById("www").onclick = function() {   

        document.getElementById("test1").innerHTML = 'MyDiv1';  
    };
</script>

<div id='test1'>This is a dummy text</div>
<a id="www" >Replace it!</a>
3
  • its document not Document at line var MyDiv1 = Document.getElementById('datum'); Commented Sep 30, 2014 at 12:54
  • use document.getElementById("test1").innerHTML = document.getElementById('datum').value; Commented Sep 30, 2014 at 12:58
  • Thanks everone for their answers! Helped me out alot! :D Commented Sep 30, 2014 at 13:07

3 Answers 3

1

You need to lowercase 'document', remove the quotes around 'MyDiv1', and get the value, not the element itself.

 <form>
Datum: <input type="text" id="datum">
</form> 

<script>

  var MyDiv1 = document.getElementById('datum');

document.getElementById("www").onclick = function() {


document.getElementById("test1").innerHTML = MyDiv1.value;  
};
</script>

<div id='test1'>This is a dummy text</div>
<a id="www" >Replace it!</a>
Sign up to request clarification or add additional context in comments.

Comments

1

There are several issues with the code:

  • It's document not Document (as Arvind pointed out)
  • You are trying to access the www element before it exists
  • You are using the string 'MyDiv1' instead of the variable MyDiv1
  • You need to use the value property of the element instead of the element itself
  • You need an href attribute on the link so that it's a link, not a bookmark
  • You need to return false from the click handler, or the link will be activated

So:

<form>
Datum: <input type="text" id="datum">
</form> 

<div id='test1'>This is a dummy text</div>
<a href="#" id="www">Replace it!</a>

<script>

var MyDiv1 = document.getElementById('datum');

document.getElementById("www").onclick = function() {
  document.getElementById("test1").innerHTML = MyDiv1.value;
  return false;
};

</script>

Comments

0
Please use below code this may be help you .

<form>
Datum: <input type="text" id="datum">
</form> 
<div id='test1'>This is a dummy text</div>

<a id="www" onclick="changetext();">Replace it!</a>

<script>

function changetext()
{
var MyDiv1 = document.getElementById("datum").value;
document.getElementById("test1").innerHTML=MyDiv1;  
}

</script>

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.