Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jun 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    .innerHTML is not working

    my code is


    var side1 = 5;
    var side2 = 6;
    var side3 = 7;
    var perimeter = (side1 + side2 + side3)/2;
    var area = Math.sqrt(perimeter*((perimeter-side1)*(perimeter-side2)*(perimeter-side3)));
    document.getElementById("dan").innerHTML= area;


    <div id="dan"></div>

    it is not show value in this tag.

    when i use document.write. its works

    var side1 = 5;
    var side2 = 6;
    var side3 = 7;
    var perimeter = (side1 + side2 + side3)/2;
    var area = Math.sqrt(perimeter*((perimeter-side1)*(perimeter-side2)*(perimeter-side3)));
    document.write(area);

    what is the problem in my above code i want to use innerHTML.

    Please Help me

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,395
    Thanks
    12
    Thanked 569 Times in 562 Posts
    The code must either

    a) be placed after the HTML markup (right before the closing </body> tag) so that getElementById() can find the element OR
    b) be wrapped in an event handler that makes sure the content DOM has been built before getElementById() is used
    Code:
    window.addEventListener('load', function() {
       // your code here
    }, false);

  • #3
    New to the CF scene
    Join Date
    Jun 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thank you devnull69

    its run.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •