...

View Full Version : document.getElementByID and innerHTML



snowieken
11-16-2004, 10:10 PM
Hello! First timer on this forum! I just started a webdevelopment course, and I have just encountered a little problem. I am fairly new to this stuff, so bear with me ;)

I wrote a function for a so-called "crumbs path". When you click on a certain <div> in the menu, it calls the function, which changes the innerHTML of a <span> somewhere on the page. So far, so good: it works perfectly in Netscape and Opera. Then I try it in IE 6. The code works, and it does exactly what it is supposed to do, but yet it gives me a syntax error. The innerHTML is changed to a pretty complicated contamination of strings, but I am pretty sure there is nothing wrong with that. Does anyone have an idea?

Here is the function:


function SwitchKruimel(text, submenu) {
if(document.getElementById) {
document.getElementById('kruimelpad').innerHTML =
'&nbsp;You are here: <a href="index.html">home</a>' +
' :: <a href="#" onClick="SwitchMenu(' +
'"' + submenu +
'");SwitchKruimel("' +
text +
'","' +
submenu +
'")">' +
text +
'</a>';
}
}

Here is the <span> which changes (in other words, the crumbs path):


<span id="kruimelpad">
&nbsp;You are here:
<a href="index.html">home</a>
</span>


And here is the function called:


<div class="menutitle" onClick="SwitchMenu('sub1');SwitchKruimel('my websites','sub1')">
- my websites -
</div>


The HTML of the crumbs path looks like this: You are here: home and it changes into You are here: home :: my websites, just as it is supposed to - but unfortunately with that nasty yellow triangle in the bottom left corner in IE. SwitchMenu() works and doesn't give me any problems, I have located the error to be in SwitchKruimel() - but that's about it. I can't find it, and some help is much appreciated! :)

adios
11-16-2004, 10:25 PM
Oddly enough, turns out 'SwitchKruimel' is a reserved word in JavaScript. You'll have to re-name that function.

Only kidding. You've got some embedded double quotes in that string, conflicting with the doubles bounding the HTML string. Use escaped singles (as you'd use unescaped ones if this were HTML)...



' :: <a href="#" onClick="SwitchMenu(' +
'\'' + submenu +
'\');SwitchKruimel(\'' +
text +
'\',\'' +
submenu +
'\')">' +


I'd clean up that entire fractured string, which is an error waiting to happen... :eek:

snowieken
11-16-2004, 10:35 PM
Oddly enough, turns out 'SwitchKruimel' is a reserved word in JavaScript. You'll have to re-name that function.

Only kidding.I know, I have a pretty nifty language. Thank you sir! :p
You've got some embedded double quotes in that string, conflicting with the doubles bounding the HTML string. Use escaped singles (as you'd use unescaped ones if this were HTML)...



' :: <a href="#" onClick="SwitchMenu(' +
'\'' + submenu +
'\');SwitchKruimel(\'' +
text +
'\',\'' +
submenu +
'\')">' +


I'd clean up that entire fractured string, which is an error waiting to happen... :eek:Yeah, it works like a charm now. I suspected it was the whole string which rendered the error, but up to now I am a champion in the "write-code-as-longwinded-as-possible"-category. Anyway, I'm glad enough that it works now. Thanks!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum