-1

I'm trying to make a money display for a form which I am making, now I found this script on the web: http://jsfiddle.net/QQGfc/ And I'm trying to implement it into my code like this: (but it's not displaying the text.)

</script>

<script type="text/javascript">
document.getElementById("MyEdit").innerHTML = "My new text!";
</script>

With this in the body

<?php
include'includes/header.php'; 
include'includes/slider.php'; //carousel
?>


<h2>Contact Information</h2>


<div id="MyEdit">
    This text will change
</div>

Ay idea whats going wrong?

2
  • 1
    In how far is the code snippet you posted similar to the fiddle you referenced? Commented Feb 10, 2014 at 21:06
  • You need to include your script element below your myEdit DIV. Or, use document.onready or window.onload to execute the script when the document is ready. Commented Feb 10, 2014 at 21:06

2 Answers 2

6

You imply that the JavaScript is in the <head>.

When it runs, the <body> hasn't been parsed, so the element you are trying to modify does not exist.

Either:

  • Move the script to after the <div> you are trying to modify.
  • Wrap the script in a function and call that function after the <div> exists (e.g. by binding it as a load event handler).
Sign up to request clarification or add additional context in comments.

4 Comments

I'm still new to this language :P Had no idea that it had to be below! Thanks a lot. :)
Hmm does this work with variables which I declared before? I want to diplay the value of the money.
The value of what? There's nothing about "the money" in the question. You might want to try-it-and-see and then ask another question if you don't get the results you expect.
The result is great, just asking a followup :P I'm trying to display a variable in the div. The variable is defined in the head above price = 150 so I'd like it to say "150"
0

you should do it after the DOM is completely loaded:

<script type="text/javascript">
    window.addEventListener("load", function(){
        document.getElementById("MyEdit").innerHTML = "My new text!";
    });
</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.