Shinu Thomas
Shinu Thomas

Reputation: 5316

Javascript - innerHTML function in IE

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?

Upvotes: 2

Views: 1882

Answers (5)

Tim Down
Tim Down

Reputation: 324587

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.

Upvotes: 0

mtelis
mtelis

Reputation: 674

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

Upvotes: 0

Shinu Thomas
Shinu Thomas

Reputation: 5316

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

Upvotes: 0

Bitsplitter
Bitsplitter

Reputation: 980

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.

Upvotes: 1

Sagar Varpe
Sagar Varpe

Reputation: 3599

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);

Upvotes: 1

Related Questions