views:

484

answers:

2

I'm having trouble using innerHTML with my radio type button.

<table align="center">
 <div class='main'>
  <span id="js" class='info'>
   <label><input type="radio" name="js" value="0" size="<?php echo $row['size']; ?>" onclick="js(this.value, this.size);" /><img src="arrowup.png"/></label>
   <br />
   <label><input type="radio" name="js" value="1" size="<?php echo $row['size']; ?>" onclick="js(this.value, this.size);" /><img src="arrowdown.png"/></label>
  </span>
 </div>
</table>

My .js looks like this:

var xmlhttp;

function getVote(a,b) {
xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)   {
  alert ("Browser does not support HTTP Request");
  return;
  }
var url="js.php";
url=url+"?js="+a;
url=url+"&id="+b;
url=url+"&sid="+Math.random();
xmlhttp.onreadystatechange=stateChanged;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}

function stateChanged() {

  if (xmlhttp.readyState==4) {
  document.getElementById("js").innerHTML=xmlhttp.responseText;
  }
}

function GetXmlHttpObject() {
var objXMLHttp=null;
if (window.XMLHttpRequest)   {
  objXMLHttp=new XMLHttpRequest();
  }
else if (window.ActiveXObject)   {
  objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
return objXMLHttp;
} 

This doesn't work in IE only! Any help?

A: 

The issue was indeed innerHTML on table tag. IE doesn't support innerHTML wrapped around a table tag nor they intend to resolve this issue in their future releases.

The workaround is to either use DOM methods or the easiest way: wrap your table around a div.

So I ended up with:

<div id="js" align="center"><table>
        <span class='main'>
            <a onClick="getVote(this.value, this.size);" type="radio" value="0" size="<?php echo $row['id']; ?>" /><img src="arrowup.png"/></a>
            <br /><a onClick="getVote(this.value, this.size);" type="radio" value="1" size="<?php echo $row['id']; ?>" /><img src="arrowdown.png"/></a>
            </span>
</table></div>

There's another IE bug that screws up the label if they're set to be image. Just a heads up! I know you won't be surprised when I say IE sucks!

froufrou
innerHTML doesn't wrap anything... IE doesn't like partially re-parsing the insides of a table, so wrap one or more tables with a div and use the div.innerHTML. Can't blame IE if you won't toss your own tag salad: <table><span>... isn't valid HTML.
machine elf
Yeah, but if it isn't valid HTML then I wonder why would it work on all other browsers ...
froufrou
For more info on this issue: http://www.ericvasilik.com/2006/07/code-karma.htmlEric is the inventor of innerHTML ... hope it helps.
froufrou
"Yeah, but if it isn't valid HTML then I wonder why would it work on all other browsers " Because of people like *you*, who naively support non-standard code. Sorry but this is simply the truth, breaking standards is the root of all evil, _especially_ on the web. One browser with big market share starts doing its own thing, then other browsers copy it, or else they lose market share.
Longpoke
A: 

firstly, pretty sure a DIV directly inside a TABLE is illegal markup.

don't use illegal markup. browsers will respond differently to it (i've recently had Opera ignore some illegal stuff with IE trying to render it).

tho IE usually deserves it, firstly get the markup correct and then see how IE does. it may be that it has failed to render the DOM how you expect.

(FWIW there is an innerHTML rendering problem i've run up against with IE, http://support.microsoft.com/kb/276228/, but for the SELECT element so i don't think it applies here).

poop-deck