everisk Posted February 10, 2008 Share Posted February 10, 2008 I would like to switch style of DIV from none to block but since there are multiple DIV in the page so I need to assign a unique number to it. My current code is not working var divid = "loadingImage"+nl; document.getElementById(divid).style.display = 'block'; The error in FireBug says "document.getElementById(divid) has no properties". Help please~ Quote Link to comment Share on other sites More sharing options...
phpQuestioner Posted February 10, 2008 Share Posted February 10, 2008 <script language="javascript"> function stylechange(divid) { document.getElementById(divid).style.display = 'block'; } </script> Quote Link to comment Share on other sites More sharing options...
everisk Posted February 10, 2008 Author Share Posted February 10, 2008 Thanks! I have another related question. I use AJAX in my script and need to show the response to a correct unique DIV. I tried creating a function just like what phpQuestioner gave "stylechange" but pass both divid and response to the function. But that doesn't seem to work. if (http.readyState == 4 && http.status == 200) { // Text returned FROM the PHP script var response = http.responseText; if(response) { // UPDATE ajaxTest content document.getElementById(divid).innerHTML = response; } } Quote Link to comment Share on other sites More sharing options...
phpQuestioner Posted February 10, 2008 Share Posted February 10, 2008 That's because you not identifying the "divid" parameter. Quote Link to comment Share on other sites More sharing options...
everisk Posted February 10, 2008 Author Share Posted February 10, 2008 what if i have it define it like this? function handleResponse(mesg) { var divid2 = "ajaxContent"+mesg; if (http.readyState == 4 && http.status == 200) { var response = http.responseText; if(response) { document.getElementById(divid2).innerHTML = response; } } } Quote Link to comment Share on other sites More sharing options...
phpQuestioner Posted February 10, 2008 Share Posted February 10, 2008 Why go to all that trouble; if your parameter is going to be static in your function anyway? Just do it like this instead: function handleResponse(mesg) { var divid2="ajaxContent2"; if (http.readyState == 4 && http.status == 200) { var response = http.responseText; if(response) { document.getElementById(divid2).innerHTML = response; document.getElementById(divid2).style.display = 'block'; } } } Quote Link to comment Share on other sites More sharing options...
everisk Posted February 10, 2008 Author Share Posted February 10, 2008 Actually it is not static .. there are multiple ajaxContent DIV on my page and i uniquely identify it with the parameter 'mesg' Quote Link to comment Share on other sites More sharing options...
phpQuestioner Posted February 10, 2008 Share Posted February 10, 2008 Actually it is not static .. there are multiple ajaxContent DIV on my page and i uniquely identify it with the parameter 'mesg' how are you setting the "mesg" parameter then? Because if your not; then you will just be getting "ajaxContent" as you div id. Quote Link to comment Share on other sites More sharing options...
mainewoods Posted February 11, 2008 Share Posted February 11, 2008 function handleResponse(mesg) { var divid2="ajaxContent2"; how are you attaching this function to the event because usually you cannot pass parameters when you define an event: xmlHttp.onreadystatechange=handleResponse; // correct xmlHttp.onreadystatechange=handleResponse(mesg); // wrong because you cannot pass parameters with an event like that, handleResponse() never recieves the parameter Quote Link to comment Share on other sites More sharing options...
everisk Posted February 11, 2008 Author Share Posted February 11, 2008 Oh that's is exactly what i'm doing xmlHttp.onreadystatechange=handleResponse(mesg); So how should I write the function so that 'mesg' will be used in handleResponse()? thanks. Quote Link to comment Share on other sites More sharing options...
mainewoods Posted February 11, 2008 Share Posted February 11, 2008 you have to make the mesg variable a global variable (created outside of any function) so it can be accessed inside the handleResponse function. 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.