homepage Welcome to WebmasterWorld Guest from 23.20.61.85
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
Javascript to Dynamically Populate Select Lists not working properly
help needed
sheetal21



 
Msg#: 4506934 posted 2:55 pm on Oct 11, 2012 (gmt 0)

I am new to JavaScripting. I have written a Javascript to dynamically populate the select list based on the selection of the option in another selection box.

The script is having 3 selection box. Depending on the selection of option in first selection box, the second selection box should be populated and depending on the selection of option in second selection box, the third selection box should be populated.

But the script I have written is giving 2 problems: 1] If the page is reloaded, the option in the first selection box selected previously remains selected and if we press the same option again to load the second selection box, the second selection box do not get loaded with its options. We have to select some other option in the first selection box and then select the option from the first selection box that we wanted to select previously.

2] If I select an option from second selection box, then the third selection box shows the options for the second selection box's selected option and it also shows some options (specially few last options) of the previously selected option from second selection box.

e.g. if second selection box has options - maharashtra, punjab, gujarat and in the third selection box i want to show Mumbai, Pune, Nagpur, Nasik, Sangli for Maharashtra option, Ludhiana, Amritsar and Patiala for Punjab option and Gandhinagar, Surat, Ahmedabad, Bhavnagar and Jamnagar. if I press maharashtra option first then all options will be shown properly, but after selecting punjab option, along with Ludhiana, Amritsar and Patiala, Nasik and Sangli option will also be shown in the third selection box.

I hope I get some help from the expert people like you. Please have a look at my script and let me know, what is wrong with it.

My javascript is as follows

<script type="text/javascript">

function selectProduct()
{
var Eclipse=new Array("e4","Incubator","JDT","Orion","PDE","Platform");

var selectedBoxValue=document.queryform.project.value;
var i;
var j;

removeSelectboxOption();
var productLength=eval(selectedBoxValue).length;

for(i=0;i<document.queryform.project.options.length;i++)
{
if(selectedBoxValue==document.queryform.project.options[i].value)
{
for(j=0;j<productLength;j++)
{
document.queryform.product.options[j]=new Option(eval(selectedBoxValue)[j],eval(selectedBoxValue)[j])
}
}
}
}

function selectComponent()
{
var e4 = new Array("Languages", "Resources", "Runtime", "Search", "SWT", "TM", "UI", "XWT","");
var Incubator = new Array("e4", "ufacekit","");
var JDT = new Array("APT", "Core", "Debug", "Doc", "Text", "UI","");
var Orion = new Array("Client", "Doc", "Editor", "Git", "OrionHub", "Releng", "Server","");
var PDE = new Array("API Tools", "Build", "Doc", "Incubators", "UI","");
var Platform = new Array("Ant", "Compare", "CVS", "Debug", "Doc", "IDE", "Incubator", "PMC", "Releng", "Resources", "Runtime", "Scripting", "Search", "SWT", "Team", "Text", "UI", "Update (deprecated - use RT\x3eEquinox\x3ep2)", "User Assistance", "WebDAV", "Website","");

var selectedBoxValue=document.queryform.product.value;
var i;
var j;

removeSelectboxOption1();
var componentLength=eval(selectedBoxValue).length;

for(i=0;i<document.queryform.product.options.length;i++)
{
if(selectedBoxValue==document.queryform.product.options[i].value)
{
for(j=0;j<componentLength;j++)
{
document.queryform.component.options[j]=new Option(eval(selectedBoxValue)[j],eval(selectedBoxValue)[j])
}
}
}
}

function removeSelectboxOption()
{
var i;
for(i=0;i<document.queryform.product.options.length;i++)
{
document.queryform.product.remove(i);
}
}

function removeSelectboxOption1()
{
var i;
for(i=0;i<document.queryform.component.options.length;i++)
{
document.queryform.component.remove(i);
}
}

</script>

div id="container_project" class="search_field_grid">
<label>Project: </label>
<select name="project" id="project" onchange="selectProduct()" style="width:200px; height:150px" multiple="multiple">
<option value="" selected="selected"> </option>
<option value="Eclipse">Eclipse </option>
</select>
</div>
<br/><br/>
<div id="container_product" class="search_field_grid">
<label>Product: </label>
<select name="product" id="product" onchange="selectComponent()" style="width:200px; height:150px" multiple="multiple">
</select>
</div>

<br/><br/>
<div id="container_component" class="search_field_grid">
<label>Component: </label>
<select name="component" id="component" style="width:200px; height:150px" multiple="multiple">
</select>
</div>


 

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved