Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / HTML, CSS, Scripts / JavaScript / June 2006



Tip: Looking for answers? Try searching our database.

Grab ElementbyID from innerhtml

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
fmdevelopertim@gmail.com - 29 Jun 2006 17:22 GMT
New to AJAX so sorry if this is simple.

Have a page that gives the user a list to select a brand. Based on the
brand selected it shows a list of sizes available using the following
code:

function handleStateChange() {
   if(xmlHttp.readyState == 4) {
       if(xmlHttp.status == 200) {
           document.getElementById("serverResponse").innerHTML =
xmlHttp.responseText;
       }
   }
}

The next step is show a list of all items available that match that
brand and size. Using the following code to grab the values selected:

function createQueryString2() {
   var Brand = document.getElementById("Brand").value;
   var Size = document.getElementById("size").value;

   var queryString = "brand=" + Brand + "&size=" + Size;

   return queryString;
}

The page works exactly as expected in FireFox, but IE is not capturing
the size element from the innerHTML. Added display vaiable functions to
the PHP page that is being rendered and the brand variable is there,
the size variable is not.

Thanks for any help.
Martin Honnen - 29 Jun 2006 17:42 GMT
> function createQueryString2() {
>     var Brand = document.getElementById("Brand").value;
[quoted text clipped - 9 lines]
> the PHP page that is being rendered and the brand variable is there,
> the size variable is not.

I can only guess from the name of that function that you somewhere call
it to create a querystring for a HTTP GET request URL.
What do you mean by "IE is not capturing the size element from the
innerHTML"? Do you create the element with id="size" by assigning to the
innerHTML of another element? Do you get a script error on
  document.getElementById("size").value
with IE? If there is no element with id="size" then that expression
should give a script error as then getElementById returns null and you
can't access .value on null. Do you get a script error?
Signature


    Martin Honnen
    http://JavaScript.FAQTs.com/

fmdevelopertim@gmail.com - 29 Jun 2006 17:48 GMT
Full Java below:

var xmlHttp;

function createXMLHttpRequest() {
   if (window.ActiveXObject) {
       xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
   }
   else if (window.XMLHttpRequest) {
       xmlHttp = new XMLHttpRequest();
   }
}

function createQueryString() {
   var Brand = document.getElementById("Brand").value;

   var queryString = "brand=" + Brand;

   return queryString;
}

function doRequestUsingGET() {
   createXMLHttpRequest();

   var queryString = "response.php?";
   queryString = queryString + createQueryString() ;
   xmlHttp.onreadystatechange = handleStateChange;
   xmlHttp.open("Post", queryString, true);
   xmlHttp.send(null);
}

function handleStateChange() {
   if(xmlHttp.readyState == 4) {
       if(xmlHttp.status == 200) {
           document.getElementById("serverResponse").innerHTML =
xmlHttp.responseText;
       }
   }
}

function createQueryString2() {
   var Brand = document.getElementById("Brand").value;
   var Size = document.getElementById("size").value;

   var queryString = "brand=" + Brand + "&size=" + Size;

   return queryString;
}

function doRequestUsingGET2() {
   createXMLHttpRequest();

   var queryString = "listing.php?";
   queryString = queryString + createQueryString2() ;
   xmlHttp.onreadystatechange = handleStateChange2;
   xmlHttp.open("Post", queryString, true);
   xmlHttp.send(null);
}

function handleStateChange2() {
   if(xmlHttp.readyState == 4) {
       if(xmlHttp.status == 200) {
           document.getElementById("available").innerHTML =
xmlHttp.responseText;
       }
   }
}

The page result.php just builds a pull down list - '<select id="size"
onchange="doRequestUsingGET2();" name="size">

I need to grab the value of <select id="Brand"
onchange="doRequestUsingGET();" name="Brand" tabindex="1"> (working
well on all browsers) and the value of Size from the innerhtml call.

These 2 values are used in listing.php which builds a table of the
results.

> > function createQueryString2() {
> >     var Brand = document.getElementById("Brand").value;
[quoted text clipped - 19 lines]
> should give a script error as then getElementById returns null and you
> can't access .value on null. Do you get a script error?
Martin Honnen - 29 Jun 2006 18:01 GMT
> function createQueryString2() {
>     var Brand = document.getElementById("Brand").value;
>     var Size = document.getElementById("size").value;

Can you answer that:

>>Do you get a script error on
>>   document.getElementById("size").value
>>with IE? If there is no element with id="size" then that expression
>>should give a script error as then getElementById returns null and you
>>can't access .value on null. Do you get a script error?

If there is no script error then IE finds the element just fine by its
id. Thus if there is no script error then the problem is not with
getElementById.

Signature

    Martin Honnen
    http://JavaScript.FAQTs.com/

fmdevelopertim@gmail.com - 29 Jun 2006 19:06 GMT
No script error.

I display the two variables in the PHP file and it displays:

Selected Brand is 19
Selected size is

The size variable is empty. It is being passed as:

   var queryString = "brand=" + Brand + "&size=" + Size;
   var queryString = "listing.php?";
   queryString = queryString + createQueryString2() ;

> > function createQueryString2() {
> >     var Brand = document.getElementById("Brand").value;
[quoted text clipped - 11 lines]
> id. Thus if there is no script error then the problem is not with
> getElementById.
Martin Honnen - 30 Jun 2006 13:03 GMT
> I display the two variables in the PHP file and it displays:
>
[quoted text clipped - 6 lines]
>     var queryString = "listing.php?";
>     queryString = queryString + createQueryString2() ;

Hard to tell without seeing a test case. Is the element with id="size" a
single or multiple select element? Does it render properly? What does
  document.getElementById('size').selectedIndex
give?

Signature

    Martin Honnen
    http://JavaScript.FAQTs.com/

fmdevelopertim@gmail.com - 30 Jun 2006 21:25 GMT
Thanks for the reply - I figured it out based on your comments. The
list rendered correctly but the value element was not correctly
defined.

I greatly appreciate your assistance!

> > I display the two variables in the PHP file and it displays:
> >
[quoted text clipped - 11 lines]
>    document.getElementById('size').selectedIndex
> give?
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.