function printcity(cityname1,tvalue){ var checkboxs = document.getElementsByName('sele'); var strcitys = document.getElementById('showcitys').value; for(var i = 0; i < checkboxs.length; i++){ if(checkboxs[i].checked == true && checkboxs[i].value==tvalue){ if(strcitys.indexOf(cityname1)<0){ strcitys+=cityname1+" "; } } else { if(strcitys.indexOf(cityname1)>=0 && checkboxs[i].value==tvalue){ // alert(strcitys); strcitys=strcitys.replace(cityname1,""); } } } // alert(tvalue); //alert(strcitys); strcitys = LTrim(strcitys); document.getElementById('showcitys').value = strcitys; } function LTrim(str){ var i; for(i=0;i<str.length; i++) { if(str.charAt(i)!=" ") break; } str = str.substring(i,str.length); return str; } <td colspan="4" class="td3" align="left"> <logic:notEmpty name="cityList"> <% List list = (ArrayList)request.getAttribute("cityList"); for(int i = 0 ;i<list.size();i++){ CityInfo ci = (CityInfo)list.get(i); %> <!-- <input type="checkbox" name="sele" value="<%=ci.getAeranumber()%>" οnclick="printcity('<%= ci.getCity()%>')"/><%= ci.getCity()%>--> <input type="checkbox" name="sele" value="<%=ci.getAeranumber()%>" οnclick="printcity('<%= ci.getCity()%>',this.value)"/><%= ci.getCity()%> <% if(i == 7){ %> <BR> <% } } %> </logic:notEmpty> </td>