jQuery in Action, 2nd edition*
The moose likes HTML, CSS and JavaScript and the fly likes multiple <html:select> elements Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "multiple <html:select> elements" Watch "multiple <html:select> elements" New topic
Author

multiple <html:select> elements

rexkk java
Greenhorn

Joined: Aug 28, 2012
Posts: 2
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

Joined: Nov 08, 2001
Posts: 15376
    
    6
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

Joined: Jan 10, 2002
Posts: 61657
    
  67

ALso, posting server-side markup in an HTML forum is likely to get you fewer responses. Always post the resulting client-side code.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: multiple <html:select> elements