We help IT Professionals succeed at work.

How to get the Jsp value in javascript

Sathish David  Kumar N
Sathish David  Kumar N asked
on
Hi ,

I have a dropdown which have value like this site(site 123) . how can i get the value of this selected drop down

if i use document.getElementById() means it give site(site after that it will drauncate
how can get full value .

how can get the full value . It will trancate after the sepace !



Comment
Watch Question

I don´t understand clearly what you need, but the blank space should not truncate the value, are you talkin about the content of:

<select id="sel" name="sel">
<option value="site(site 123)">site(site 123)</option>
</select>

can you share the jsp code that you are trying with? As @cesardanielleon is saying it should not truncate after space.

did you tried something like this for the name or value

<select id="sel" name="sel">
<option value="site(site <%= //jsp code %>)">site(site 123)</option>
</select>

<select id="sel" name="sel">
<option value="site(site 123)>site(site 123)</option>
<option value="site(site456)>site(site456)</option>
</select>
 i want to get the selected value in javascript ?
if i select site(site456) and in java script function using document.getElementById()
i get the correct result  site(site456)
but i choose site(site 123) means i am getting the site(site upto this only .
check this;you missed quotes in the option value.


<script>

function ex()
{

alert(document.frm.sel.options[document.frm.sel.options.selectedIndex].value)
}

</script>
<form name="frm">
<select id="sel" name="sel" onChange="ex()">
<option value="site(site 123)">site(site 123)</option>

<option value="site(site456)">site(site456)</option>
</select>
</form>
if you want to fetch through id you can do like this;

alert(document.getElementById("sel").options[document.getElementById("sel").options.selectedIndex].value)
@dravidnsr: you haven't shared the jsp code that you have written. Please share the  same so that we can help you.

Also, please check if the quotes are given properly as a fellow expert has also mentioned.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="/WEB-INF/displaytag-11.tld" prefix="display" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%>
<%@ taglib uri="http://java.sun.com/jstl/core-rt" prefix="c-rt"%>
<%@ page import="com.merck.mrl.iftrs.common.beans.User,com.merck.mrl.iftrs.depotMain.beans.DepotMaintainBean,java.util.Iterator,com.merck.mrl.iftrs.form.depot.DepotAncilaryTransBean"%>
<%@ page import="java.util.ArrayList" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>MRL-IFTRS</title>
</head>
<link rel="stylesheet" type="text/css" href="css/screens_new.css">
<link rel="stylesheet" type="text/css" href="css/style.css">
<script type="text/javascript">
var xmlhttp;
var context='<%= request.getContextPath() %>';
var a=/^ *[0-9]+ *$/ ;
function initRequest()
{
if (window.XMLHttpRequest)
{
xmlhttp = new XMLHttpRequest();
}
else if (window.ActiveXObject)
{
isIE = true;
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
}

function getSite()
{

initRequest();
var studyNo=document.getElementById("studyNo").value;
var url = context+"/AjaxUtilAction.do";
url = url + "?callType=listOfSitesMappedToSite";
url = url + "&studyNo="+ studyNo;
xmlhttp.open("GET", url, true);
xmlhttp.onreadystatechange = fetchSite;

xmlhttp.send();
}

function getShipNo()
{

initRequest();


var siteId = document.getElementById("siteId");
var siteId1 = siteId.options[siteId.selectedIndex].value;

alert(siteId1);
var studyNo=document.getElementById("studyNo").value;

return false;
var url = context+"/AjaxUtilAction.do";
url = url + "?callType=listOfShipNoToSite";
url = url + "&studyNo="+ studyNo;
url = url + "&siteId="+ siteId;
xmlhttp.open("GET", url, true);
xmlhttp.onreadystatechange = fetchShipNo;

xmlhttp.send();
}

 
function fetchSite()
{
if(xmlhttp.readyState == 4)
{
var rText = xmlhttp.responseText;

var records=rText.split(",");

var fieldvalue;
var i=0,j=0;
var placecontrol="<select name=siteId id=siteId onchange=getShipNo() > <option value=Select>Select</option>";
for(i=0;i<records.length-1;i++)
{
fieldvalue=records[i].split(",");

placecontrol+="<option value="+fieldvalue[0]+">"+fieldvalue[0]+"</option>";
}
placecontrol+="</select>";

document.getElementById("SourceSiteList").innerHTML=placecontrol;

document.getElementById("SourceSiteListDefault").style.display='none'; //hide drop down
}
}
function fetchShipNo()
{
if(xmlhttp.readyState == 4)
{
var rText = xmlhttp.responseText;

var records=rText.split(",");

var fieldvalue;
var i=0,j=0;
var placecontrol="<select name=shipNo id=shipNo > <option value=Select>Select</option>";
for(i=0;i<records.length-1;i++)
{
fieldvalue=records[i].split(",");

placecontrol+="<option value="+fieldvalue[0]+">"+fieldvalue[0]+"</option>";
}
placecontrol+="</select>";

document.getElementById("ShipNoList").innerHTML=placecontrol;
document.getElementById("ShipNoListDefault").style.display='none'; //hide drop down
}

}

function numValidation(tableID,i){


var table = document.getElementById(tableID);
var myElements = table.getElementsByTagName("input");
var trlen = table.rows.length;
var check=parseInt(i)+1;
var x=document.getElementById(tableID).rows[check].cells;
var recQTY=parseInt(document.getElementById('receviedQTY'+i).value);
if(!a.test(recQTY)){alert("Please Enter Correct Recevied QTY ");}
else{

var shippedQTY=(parseInt(x[3].innerHTML));
var totQTY=shippedQTY-recQTY;
if(totQTY<0)
{
alert("Please Enter Recevied QTY is less than Shipped QTY");
}
else{document.getElementById('notRecQTY'+i).value=totQTY;}
}


return false;
}
function demagedQTYValidation(tableID,i)
{
if(!a.test(parseInt(document.getElementById('demageQTY'+i).value))){alert("Please Enter Correct demaged QTY ");}
}
function saveMethod(tableID)
{
var table = document.getElementById(tableID);
var myElements = table.getElementsByTagName("input");
var trlen = table.rows.length;
var j=0;
var str = new String();
try{
for(i=1; i<trlen; i++)
{

var x=document.getElementById(tableID).rows[i].cells;
var check=parseInt(i)-1;

var type = ackAncilForm.elements[i-1].type;
if (document.getElementById('cbox'+check).checked==true){

if(trlen== 2)
{

if((document.getElementById('receviedQTY').value)=="0"){throw 'recQTY';}
str = str + x[1].innerHTML+','
+ x[2].innerHTML+','
+ x[3].innerHTML+','
+ x[4].innerHTML+','
+ x[5].innerHTML+','
+ document.getElementById('receviedQTY').value+','
+ document.getElementById('notRecQTY').value+','
+ document.getElementById('demageQTY').value+','+'0!';
}
else
{
j=j+1;
if((document.getElementById('receviedQTY'+check).value)=="0"){throw 'recQTY';}
str = str + x[1].innerHTML+','
+ x[2].innerHTML+','
+ x[3].innerHTML+','
+ x[4].innerHTML+','
+ x[5].innerHTML+','
+ document.getElementById('receviedQTY'+check).value+','
+ document.getElementById('notRecQTY'+check).value+','
+ document.getElementById('demageQTY'+check).value+','+'0!';

}

}

}
if (j==0){throw 'selectAcn';}
}catch(er){

if(er=="recQTY"){alert("Please Enter Correct Recevied Qty"); return false; }
if(er=="selectAcn"){alert("Please Select any one Ancliary"); return false; }

}

document.ackAncilForm.selectedAncil.value=str;
document.ackAncilForm.action=context+"/ackAncilShip.do?function=Save";
document.ackAncilForm.submit();


}

 
 
</script>
<body>
<jsp:include page="/jsp/common/header_new.jsp" />
<div align="left">

<jsp:include page="/jsp/common/Menu_new.jsp" />
</div>
<font color="red"><html:errors /></font>
<div align="center">

<fieldset style="width: 90%"> <legend class="legendHeader" style="background-color: "><b>Acknowledge the Ancillary Received at Site</b></legend>
<html:form action="/ackAncilShip">
<table align="center">
<tr>
<td align="left"><bean:message key="title.caf.invoice.studyname"/></td>
<td align="left">

<%

ArrayList studyNo = (ArrayList)request.getAttribute("listAncillaryStuNumbers");
if (studyNo != null && studyNo.size() > 0){
out.println("<CENTER>");
%><select name="studyNo" onchange="getSite()" id="studyNo">
<option value='0'>Select</option>
<%
for (int i=0;i<studyNo.size();i++) {

out.println("<option value='"+studyNo.get(i) + "'");
if(request.getSession().getAttribute("listAncillaryStuNumbersSes")!= null)
{
if(((String)request.getSession().getAttribute("listAncillaryStuNumbersSes")).equals(studyNo.get(i)))
{
out.println("selected='selected'");
}
}

out.println(">"+ studyNo.get(i) + "</option>");
}%>
</select><%
out.println("</CENTER>");

}



%>

</td>

</tr>
<tr>
<td align="left"><bean:message key="label.ancillaryShip.shipment.ship"/></td>
<td align="left">

<div id="SourceSiteList" ></div>
<div id="SourceSiteListDefault" align="center" > 
<select name="siteId1" id="siteId1" > 
<option value="select">Select</option>

</select>
</div>

</td>
</tr>
<tr>
<td align="left"><bean:message key="label.ancillaryShip.shipment"/></td>
<td align="left">

<div id="ShipNoList" ></div>
<div id="ShipNoListDefault" align="center" > 
<select name="shipNoList1" id="shipNoList1" > 
<option value="select">Select</option>

</select>
</div>

</td>
</tr>
<tr> <td><html:submit value="Submit"/></td>
</tr>
</table>
</html:form>
</fieldset>
<%

String studyNo = session.getAttribute("listAncillaryStuNumbersSes")==null?"":(String)session.getAttribute("listAncillaryStuNumbersSes");
String siteId = session.getAttribute("siteIdSes")==null?"":(String)session.getAttribute("siteIdSes");
String shipNo = session.getAttribute("shipNoSes")==null?"":(String)session.getAttribute("shipNoSes");

%>

<%if(!studyNo.equals("")){ %>
<fieldset style="width: 90%">
<table>
<tr><td><font color="blue" size="2">Study : </font><font size="2"><%=studyNo%></font></td><td></td>
<td><font color="blue" size="2">Site : </font><font size="2"><%=siteId%></font></td><td></td>
<td><font color="blue" size="2">Shipped No : </font><font size="2"><%=shipNo%></font></td></tr>
</table>
</fieldset>
<%} %>

<c-rt:if test="<%= request.getAttribute("fetchShipedAncilList") != null && 
((ArrayList)request.getAttribute("fetchShipedAncilList")).size() > 0 %>">
<fieldset style="width: 90%">

<legend align="left" style="vertical-align: middle"><b>
<bean:message key="label.ancillaryList"/></b>
</legend>
<form name="ackAncilForm" method="post">

<% int i =0; %>
<display:table name="fetchShipedAncilList" pagesize="20" align="center"
cellpadding="5" class="data" requestURI="/ackAncilShip" id="processTable" > 
<display:column>
<INPUT TYPE="checkbox" name="cbox<%=i%>" id="cbox<%=i%>" ></INPUT>
</display:column>
<display:column property="achilName" title="Ancillary Name" align="center"/>
<display:column property="batchNo" title="Batch No" align="center" />
<display:column property="shipedQTY" title="Shipped Quantity" align="center" />
<display:column property="expDate" title="Expiry Date" align="center" />
<display:column property="invoiceNo" title="invoiceNo" align="center" class="hidden" headerClass="hidden" media="html" />
<display:column title="Recevied Quantity" > 
<input type="text" name="receviedQTY" Id="receviedQTY<%=i%>" value="0" onblur="numValidation('processTable','<%=i%>')">
</display:column>
<display:column title="Demaged Quantity" > 
<input type="text" name="demageQTY" Id="demageQTY<%=i%>" value="0" > 
</display:column>
<display:column title="Not Recevied Quantity" > 
<input type="text" name="notRecQTY" Id="notRecQTY<%=i%>" value="0" readonly="readonly">
</display:column>
<%=i++ %>
</display:table>

<table width="100%">
<tr>
<td align="left" width="10%">
</td>
<td align="left" width="10%">
<input type="hidden" name="selectedAncil" Id="selectedAncil" > 
</td>
<td align="center" colspan="9"><br/><br/> <input type="button" name="Get Ancillaries " 
value="Get Ancillaries" onclick="saveMethod('processTable') "></input>
</td>

<td align="left" width="10%">

</td>
<td align="left" width="15%">

</td>

</tr>
</table>


<input type="hidden" name="studyNo" value="<%=studyNo%>">
<input type="hidden" name="siteId" value="<%=siteId%>">
<input type="hidden" name="shipNo" value="<%=shipNo%>">

</form>


</fieldset>
</c-rt:if>
<center>
<c-rt:if test="<%= request.getAttribute("depotTransList") != null && 
((ArrayList)request.getAttribute("depotTransList")).size() <= 0 %>">
<font color="red"> <c-rt:out value="No Records Exists for this invoice."></c-rt:out></font>
</c-rt:if>
</center>
</div>
<%@ include file="/jsp/common/footer.jsp" %>
</body>
</html>
alert.JPG
put single quotes wherever option values exists.before double quotes put single quotes

placecontrol+="<option value= ' "+fieldvalue[0]+" '>"+fieldvalue[0]+"</option>";
Gr8 answer Man ,

i am looking this for last 2 weeks . When wrongly check so that only i didnt give points early.
Thanks alot man