Jump to content

Changing display style element


everisk

Recommended Posts

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~

Link to comment
https://forums.phpfreaks.com/topic/90317-changing-display-style-element/
Share on other sites

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;
      }
  }

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;
      }
  }
}

 

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';
      }
  }
}

 

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.

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.