Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.