Avatar of emsysindia
emsysindia
 asked on

Ajax response contains combobox unable to assign to html <div>

Hi,
I am working on jsp page. From servlet class I am retrieving the combobox as

Servlet class:

String buffer=;
buffer=buffer+<option >hi</option>;
buffer=buffer+<option >bye</option>;



Jsp file:
function getSearch()
{

xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
  {
  alert ("Your browser does not support AJAX!");
  return;
  }

xmlHttp.onreadystatechange=stateChangedSearch;
xmlHttp.open("POST","servlet_className?value="+99,true);
xmlHttp.send(null);
}



function stateChangedSearch ()
{
      
if (xmlHttp.readyState==4)
{
      
document.getElementById("loc").innerHTML=xmlHttp.responseText;
}
}


<body>
<select id=t1 name=t1>
<span size="100" id=" loc" >
 </span>
</select>

</body>


But the <option> String value from ajax script response  is not allocating to <div id=loc>, showing error object required.


JavaScriptAJAXJSP

Avatar of undefined
Last Comment
Murali Murugesan

8/22/2022 - Mon
Murali Murugesan

are you trying to set the option html to the span tag or to div tag.?

emsysindia

ASKER
oh sorry , thats <div tag>
<body>
<select id="t1" name="t1">
<div size="100" id=" loc" >
 </div>
</select>

</body>

Murali Murugesan

try this,

<div size="100" id=" loc" >
 </div>

and form the below content in the servlet
<select id="t1" name="t1">
<option >hi</option>
<option >bye</option>
</select>

then replace the inner contents  of div tag.

<select> cannot have <span> tag inside it and it can have only <option> tag.

Your help has saved me hundreds of hours of internet surfing.
fblack61
Murali Murugesan

or else have this in the html

<select id="t1" name="t1">
</select>


function stateChangedSearch ()
{
     
if (xmlHttp.readyState==4)
{
     
document.getElementById("t1").innerHTML=xmlHttp.responseText; // replace innerHTML of select tag
}
}

emsysindia

ASKER
Yours idea is correct, but below code
<select id="t1" name="t1">
<option >hi</option>
<option >bye</option>
</select>

from servlet  will be shown after ajax reponse only, before to ajax response the combobox portion/place in the web page  will be blank.
 
 
Murali Murugesan

small modification to my previous post,

<div id="loc">
<select id="t1" name="t1">
</select>
</div>

and form the below content in the servlet
<select id="t1" name="t1">
<option >hi</option>
<option >bye</option>
</select>

then replace the inner contents  of div tag.

function stateChangedSearch ()
{
     
if (xmlHttp.readyState==4)
{
     
document.getElementById("loc").innerHTML=xmlHttp.responseText; // replace innerHTML of select tag
}
}

-Murali*
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
emsysindia

ASKER
I tried
document.getElementById("t1").innerHTML=xmlHttp.responseText; // replace innerHTML of select tag
But still not allocating.
 
 
 

 
emsysindia

ASKER
thnx murali, its working but still I am in doubt,
if m not wrong den  there are two <select> tags(one from servlet class and another in jsp page ) , how its working.
ASKER CERTIFIED SOLUTION
Murali Murugesan

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Murali Murugesan

Hi,

Are you looking for anyother response? if not, do u mind closing this question?

-Murali*
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy