2

I have a html file with content

<html>
<body>
<script language="JavaScript">
    function fun1()
    {
        alert(document.getElementById('id1').innerHTML);
    }
</script>
<div id="id1">
    <ul>
        <li>Here111</li>
        <li>Here222</li>
        <li>Here333</li>
    </ul>
</div>
<a href="javascript:void(0)" onclick="fun1()">Click</a>
</body>
</html>

In innerHTML function iam getting only

<ul>
    <li>Here111
    <li>Here222
    <li>Here333</li>
</ul>

The </li> tag is missing how can I get entire content?

3
  • As you claim it does not work for you - post your entire page, there must be some other thing that does not work. @Bitsplitter wrote a page that works fine for IE 6-8. Commented Nov 16, 2010 at 8:02
  • I see that I did not evaluate my own solution properly. I was concentrating on seeing the </ul> that is missing in your original question. That appears in IE just fine but you are right, the first </li> is missing in my sample too in both IE7 and 8. Commented Nov 16, 2010 at 8:09
  • Is their any solution for this Commented Nov 16, 2010 at 8:51

5 Answers 5

1

You have set id of div as a "test" but you are getting it by "test1"

alert(document.getElementById('test1').innerHTML);

it should be

alert(document.getElementById("test").innerHTML);
Sign up to request clarification or add additional context in comments.

Comments

1

Your script refers to an id called test1 but your html hasn't got any.

If you do it like this:

<html>
    <head>
        <script>
            function test(){
                alert(document.getElementById('test1').innerHTML);
            }
        </script>
    </head>
    <body>
        <div id="test1">
            <ul>
                <li>This is first</li>
                <li>This is Second</li>
            </ul>
        </div>
        <a href="#" onclick="test();">hhhhhh</a>
    </body>
</html>

it works fine even in IE 7/8.

Comments

0

Add <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> in the beginning of html

1 Comment

If switching to standards mode fixed this, then you should run your HTML through an HTML validator because it probably means you have some semi-serious errors somewhere in it.
0

There also is outerHTML property (unfortunately, unsupported in some browsers). It returns the whole of element.

Comments

0

This is not incorrect. For <li> elements, the closing tag is optional in HTML so IE is returning a valid fragment of HTML here (not that it always does).

If you need closing tags, you'll need to do create your own HTML string by traversing the DOM.

2 Comments

Given the above HTML doc, you might argue that it is not incorrect (albeit horribly inconsistent on IE's part since it does close the last li explicitly...). However, IE will give the same innerHTML when using any XHTML doctype as well, in which case I'd like to argue that it IS wrong.
@Gustav: I disagree: IE doesn't support XHTML. It parses XHTML exactly the same as it parses HTML.

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.