[RESOLVED] JavaScript innerHTML Problem
Why does
Code:
theMenuSection.innerHTML='<ul>';
alert(theMenuSection.innerHTML);
Produce
?
This is very annoying since the list has a child list inside it and is constructed as the page is loading. Is there any way to prevent JavaScript from closing off the <ul> tag?
theMenuSection is a div tag (also tried span).
Re: JavaScript innerHTML Problem
What is the complete code?
Re: JavaScript innerHTML Problem
This code here will produce the same problem:
Code:
<span id="testspan"></span>
<br />
<script>
var theMenuSection
theMenuSection=document.getElementById("testspan");
theMenuSection.innerHTML='<ul>';
alert(theMenuSection.innerHTML);
</script>
Copy paste it into a text file, and rename to htm, and open it.
The code that I'm doing it in is a bit long to send.
By the way, I'm using Firefox v3.
Re: JavaScript innerHTML Problem
I don't quite understand what it is you are trying to do whether you want:
1. Just have the opening <ul> tag.
or
2 Have the alert display the text within the <ul> tags?
Re: JavaScript innerHTML Problem
I want to have just an opening <ul> tag inside theMenuSection.
But for some unknown reason, if you place in a <ul> tag, it also automatically closes the <ul> tag. Run the sample code I placed up and you will see what I mean.
I need the tag to remain open so that I can have another javascript function create a nested <ul> tag inside the parent one (For a nested list).
Basically, I need the HTML inside theMenuSection, to be what I want it to be, not having a </ul> in it, like is being placed in. So option 1.
Now, I really have no idea why it auto-closes the tag, and why on earth the developers would make it do this.
I need either an alternative (Something other then innerHTML), or a solution (Maybe a parameter somewhere)?
Re: JavaScript innerHTML Problem
Code:
<html>
<span id="testspan"></span>
<br />
<script>
var theMenuSection
theMenuSection=document.getElementById("testspan");
theMenuSection='<ul>';
alert(theMenuSection);
</script>
</html>
Re: JavaScript innerHTML Problem
Actually, wait.
That don't work. If you do that, then theMenuSection no longer refers to the span/div tag.
Re: JavaScript innerHTML Problem
I mean, you can no longer refer to the span/div tag. So how can this be achieved (The auto-completing turned off)?
Re: JavaScript innerHTML Problem
Try using the right or left function to subtract the length tag from that of innerHTML.
Re: JavaScript innerHTML Problem
I already did that, but it still puts it there (It must put it there when it actually writes it to the webpage).
Maybe it's not possible, sooo... is there any other thing I can use to place html onto the page?
Re: JavaScript innerHTML Problem
Quote:
Originally Posted by Slyke
I already did that, but it still puts it there (It must put it there when it actually writes it to the webpage).
Maybe it's not possible, sooo... is there any other thing I can use to place html onto the page?
Just a thought have you disabled the "code hints" in your html editor (if possible)?
Edit:
Code:
<html>
<span id="testspan"></span>
<br />
<script>
var theMenuSection
theMenuSection=document.getElementById("testspan");
theMenuSection.html='<ul>';
alert(theMenuSection.html);
</script>
</html>
Re: JavaScript innerHTML Problem
You're a genius! That worked a charm :).
Can't disable "code hints" because this is going in an instruction manual for some online software (Everyone reading it would have to disable it right?).
Re: JavaScript innerHTML Problem
Quote:
Originally Posted by Slyke
You're a genius! That worked a charm :).
Can't disable "code hints" because this is going in an instruction manual for some online software (Everyone reading it would have to disable it right?).
For some reason not sure why the following happen:
Code:
.html = open tag
.innerHTML = <ul></ul>
.outerHTML = <span id="testspan"></span>