Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

multiple <html:select> elements

 
rexkk java
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi i have a critical issue,
Below is my jsp code:
<html:select property="city" name="city" onchange="javascript:checkCity(this);">
<html:option value="N">NewYork</html:option>
<html:option value="F">France</html:option>
<html:option value="I">Italy</html:option>
<html:option value="P">Paris</html:option>
</html:select>

Note:The above <html:select> will be placed in a for loop

Below is my javascript code:
ar citySelected = new Array();
function checkCity(selObject)
{
var form = document.forms[0];
var cityObj = form["type"];
var len = cityObj.length;
if(selObject==cityObj) // if there is single <html:select> selObject is same as city Object.so this logic works fine
{
if(cityObj.value==cityObj.options[3].value)
{
alert("You have selected Paris City");
citySelected[0] = true;
}
if(!cityObj.options[3].selected && cityObj[0])
{
var result = confirm("You have selected cities other than paris");
if(result)
{
citySelected[0] = false;
}
else
{
cityObj.options[cityObj.options.selectedIndex].selected=false;
cityObj.options[3].selected=true;
}
}
}
else{
for(var i=0; i<len; i++) { //if there are multiple <html:select> then take length of form object n iterate
if (selObject == cityObj[i] )
{
if(cityObj[i].value==cityObj[i].options[3].value) // if 3rd option is selected
{
alert("You have selected Paris City");
citySelected[i] = true;
}
if(!sctypeObj[i].options[3].selected && citySelected[i]) //if 3rd option is deselected
{
var result = confirm("You have selected cities other than paris");
if(result)
{
cityObj[i] = false;
}
else
{
cityObj[i].options[cityObj[i].options.selectedIndex].selected=false;
cityObj[i].options[3].selected=true;
}
}
}
}
}
}

Below is my javascript which works onload
function onload()
{
var form = document.forms[0];
var formObj = form["type"];
var size=formObj.size;
var len = formObj.length;

for(var i=0; i<len; i++) {
citySelected[i] = false;
}
if(size==0){ //if there is seingle <html:select> element
var cityvalue=formObj.value;
if(cityvalue=="P")
{
citySelected[0] = true;
}
}
else
{
for(var i=0; i<len; i++) { //if there are multiple <html:select> elements
var cityvalue=formObj[i].value;
if(cityvalue=="P")
{
citySelected[i] = true;
}
}
}
}

Here is where am finding problem.Onload if there is single or multiple elements the logic works fine.But when there are no elements at all in my jsp pr say if i have option to delete all dropdowns then my jsp throws javascript error."size is null or not an object".

How do i resolve this?in onload() function am differentiating btwn element using size.

if(size==0)
{
//logic for single <html:select>
}
else
{
//logic for multiple html select
}


But when there are no elements at all in my jsp pr say if i have option to delete all dropdowns then my jsp throws javascript error."size is null or not an object". How do i resolve this.Any help would be great..
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use code tags when posting code.

Check to see if the length is greater than zero before checking the options.

Eric
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64633
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ALso, posting server-side markup in an HTML forum is likely to get you fewer responses. Always post the resulting client-side code.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic