Michan Posted April 30, 2008 Share Posted April 30, 2008 Hi, I'm stuck on a bit of code. I have more than one <div>, and depending on which "comic" you choose, I'd like to load content into the specified <div>. How would I get about doing it? The code pretty much explains itself below. <script type="text/javascript"> var xmlHttp function selectcomic(str,div) { xmlHttp=GetXmlHttpObject(); if (xmlHttp==null) { alert ("Your browser does not support AJAX!"); return; } var url="comics.php"; url=url+"?getresult="+str; xmlHttp.onreadystatechange=stateChanged(div); xmlHttp.open("GET",url,true); xmlHttp.send(null); } function stateChanged(div) { if (xmlHttp.readyState==4) { document.getElementById(div).innerHTML=xmlHttp.responseText; } } function GetXmlHttpObject() { var xmlHttp=null; try { // Firefox, Opera 8.0+, Safari xmlHttp=new XMLHttpRequest(); } catch (e) { // Internet Explorer try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); } } return xmlHttp; } </script> Comic: <a href="" onclick="selectcomic(\'1\',\'div1\'); return false">Comic 1</a> <a href="" onclick="selectcomic(\'2\',\'div2\'); return false">Comic 2</a> <a href="" onclick="selectcomic(\'3\',\'div3\'); return false">Comic 3</a> <div id="div1">None selected!</div> <div id="div2">None selected!</div> <div id="div3">None selected!</div> Thanks in advance! Quote Link to comment Share on other sites More sharing options...
markjoe Posted May 1, 2008 Share Posted May 1, 2008 Wrong: xmlHttp.onreadystatechange=stateChanged(div); That line is assigning the return value of stateChanged(div), not assigning the function to the handler. Right: xmlHttp.onreadystatechange=stateChanged; Notice the problem? You cannot pass variables this way. Try this: xmlHttp.onreadystatechange=function(){ if (xmlHttp.readyState==4){ document.getElementById(div).innerHTML=xmlHttp.responseText; } }; This way you are assigning the code itself as a function type property (or something like that) and still within the scope of the selectcomic function. Quote Link to comment Share on other sites More sharing options...
Michan Posted May 1, 2008 Author Share Posted May 1, 2008 Thank you so much; it works! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.