...

View Full Version : Comparing Strings and innerHTML results in IE



johnnyb
06-04-2006, 06:56 PM
Hi,

I have this function which works great in Firefox, but doesn't work in IE. I can make it work in IE by breaking it in Firefox, but I'd like it to work in both.

Here's the function, with associated code:


function validate_check(input, message)
{
var par = input.parentNode;
var prev = input.previousSibling;

if(!input.checked) {
if(prev.innerHTML != message) {
var errmsg = document.createElement("p");
errmsg.innerHTML = message;
par.insertBefore(errmsg, input);
}

dosubmit = false;
} else if(prev.innerHTML == message) {
par.removeChild(prev);
}

}

var agree = document.getElementById('agree');
var agreemsg = 'Please make sure you agree to our <a href="/tou.php" target="_blank">Terms of Use</a> and <a href="/privacy.php" target="_blank">Privacy Policy</a> to add your listing.';

validate_check(agree, agreemsg);


For the two attributes the function accepts, input is a node from that document tree that is a checkbox, and message is a string.

If the checkbox is not checked the function inserts a <p> element immediately before the input. That part works fine in both. However, the 'prev.innerHTML == message' and 'prev.innerHTML != message' don't work properly in IE. I think it is because of the <a> tags in 'message'. However, when I try to use the String() function there are problems because the 'prev' object is not always set, (like, if a paragraph isn't written right before the checkbox).

Does anyone know how to make this work properly in both browsers?

Thanks a lot.

John

Kravvitz
06-05-2006, 03:14 AM
Why are you comparing the innerHTML to the string?

Perhaps IE isn't counting text nodes that only contain white space characters as the previousSibling but FF is.
After

var prev = input.previousSibling;add

while((prev.nodeType != 1) && prev.previousSibling) {
prev=prev.previousSibling;
}

johnnyb
06-05-2006, 03:20 AM
What do you know - it works!

Thanks a lot.

John



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum