Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Apr 2003
    Location
    Montreal, QC
    Posts
    340
    Thanks
    3
    Thanked 2 Times in 2 Posts

    Comparing Strings and innerHTML results in IE

    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:
    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
    Search for Laughter or Just Search?
    GiggleSearch.org
    Blog: www.johnbeales.com
    All About Ballet: www.the-ballet.com

  • #2
    Senior Coder
    Join Date
    Feb 2006
    Location
    USA
    Posts
    1,013
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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
    Code:
    var prev = input.previousSibling;
    add
    Code:
    while((prev.nodeType != 1) && prev.previousSibling) {
      prev=prev.previousSibling;
    }
    Learn CSS. | SSI | PHP includes | X/HTML Validator | CSS validator | Dynamic Site Solutions
    Java != JavaScript && JScript != JavaScript
    Design/program for Firefox (and/or Opera), apply fixes for IE, not the other way around.

  • #3
    Regular Coder
    Join Date
    Apr 2003
    Location
    Montreal, QC
    Posts
    340
    Thanks
    3
    Thanked 2 Times in 2 Posts
    What do you know - it works!

    Thanks a lot.

    John
    Search for Laughter or Just Search?
    GiggleSearch.org
    Blog: www.johnbeales.com
    All About Ballet: www.the-ballet.com


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •