| | |
how can i pass a value from java script to JSP scriplet
Please support our JSP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
JAVASCRIPT
<script language="JavaScript">
function show(id)
{
if (document.getElementById(id).style.display == 'none')
{
document.getElementById(id).style.display = '';
}
}
function hide(id)
{
document.getElementById(id).style.display = 'none';
}
function change(type,countryId,tableId1, tableId2,tableId3)
{
if(type == 'RACE'){
if(countryId == 'BUMI' || countryId == 'CHI' || countryId == 'IND' ||countryId == 'BSBH'|| countryId == 'BSWK' )
{
//alert('in');
show(tableId1);hide(tableId2);hide(tableId3);show('ethnicTbl'); hide('err');
}
else{
//alert('out');
show(tableId2);hide(tableId1); hide(tableId3); hide('ethnicTbl');show('err');
}
}
else if (type=='ADD' || type=='NATIONALITY'){
if(countryId == '222')
{
//alert('in');
show(tableId1); hide(tableId2); hide(tableId3);
}
else{
//alert('out');
show(tableId2); hide(tableId1); hide(tableId3);
}
}
}
function sendToBean(type,val){
alert(val + type);
if(type == 'pob'){
document.pobForms.pob3.value = val;
}
}
</script>
in JSP
[html]
<html:select name="applicationVO" property="nationalityId" onchange="change('NATIONALITY',this.options[this.selectedIndex].value,'pobStateTbl','pobStateIntTbl','');">
<html:options collection="nationlv" name="nationVO" property="nationalityId"
labelName="nationVO" labelProperty="nationalityName" />
</html:select>
<table id="pobStateIntTbl" style="display:none;" cols=1 cellpadding=2>
<tbody><tr valign=top align=left><td valign=baseline></td> <td width=1 style='width:250px;'>
<html:select property="pob" onchange="sendToBean('pob',this.value);">
<html:options collection="countrylvPob" name="countryVO" property="countryName"
labelName="countryVO" labelProperty="countryName" />
</html:select>
</td></tr> </tbody> </table>
<table id="pobStateTbl" style="display:none;" cols=1 cellpadding=2>
<tbody> <tr valign=top align=left> <td valign=baseline></td> <td width=1 style='width:250px;'>
<html:select property="pob" onchange="sendToBean('pob',this.value);">
<html:options collection="statelvPob" name="stateVO" property="name"
labelName="stateVO" labelProperty="name" />
</html:select>
</td> </tr></tbody> </table>
<c:choose> <c:when test="${applicationVO.nationalityId == '222' }">
<script type="text/javascript">change('NATIONALITY','222','pobStateTbl','pobStateIntTbl','');</script>
</c:when> <c:otherwise>
<script type="text/javascript">change('NATIONALITY','221','pobStateTbl','pobStateIntTbl','');</script>
</c:otherwise> </c:choose>
<form name="pobForm">
<input type="hidden" name="pob3" value="">
</form>
[/html]
THE PROBLEM(s)
1. Only 1st table (id=pobStateIntTbl) is sent to action.java when submit();
2. Javascript seems don't send value to pobForm.pob3.value (hidden field) and it is declare using html not jstl.
Thank you..
<script language="JavaScript">
function show(id)
{
if (document.getElementById(id).style.display == 'none')
{
document.getElementById(id).style.display = '';
}
}
function hide(id)
{
document.getElementById(id).style.display = 'none';
}
function change(type,countryId,tableId1, tableId2,tableId3)
{
if(type == 'RACE'){
if(countryId == 'BUMI' || countryId == 'CHI' || countryId == 'IND' ||countryId == 'BSBH'|| countryId == 'BSWK' )
{
//alert('in');
show(tableId1);hide(tableId2);hide(tableId3);show('ethnicTbl'); hide('err');
}
else{
//alert('out');
show(tableId2);hide(tableId1); hide(tableId3); hide('ethnicTbl');show('err');
}
}
else if (type=='ADD' || type=='NATIONALITY'){
if(countryId == '222')
{
//alert('in');
show(tableId1); hide(tableId2); hide(tableId3);
}
else{
//alert('out');
show(tableId2); hide(tableId1); hide(tableId3);
}
}
}
function sendToBean(type,val){
alert(val + type);
if(type == 'pob'){
document.pobForms.pob3.value = val;
}
}
</script>
in JSP
[html]
<html:select name="applicationVO" property="nationalityId" onchange="change('NATIONALITY',this.options[this.selectedIndex].value,'pobStateTbl','pobStateIntTbl','');">
<html:options collection="nationlv" name="nationVO" property="nationalityId"
labelName="nationVO" labelProperty="nationalityName" />
</html:select>
<table id="pobStateIntTbl" style="display:none;" cols=1 cellpadding=2>
<tbody><tr valign=top align=left><td valign=baseline></td> <td width=1 style='width:250px;'>
<html:select property="pob" onchange="sendToBean('pob',this.value);">
<html:options collection="countrylvPob" name="countryVO" property="countryName"
labelName="countryVO" labelProperty="countryName" />
</html:select>
</td></tr> </tbody> </table>
<table id="pobStateTbl" style="display:none;" cols=1 cellpadding=2>
<tbody> <tr valign=top align=left> <td valign=baseline></td> <td width=1 style='width:250px;'>
<html:select property="pob" onchange="sendToBean('pob',this.value);">
<html:options collection="statelvPob" name="stateVO" property="name"
labelName="stateVO" labelProperty="name" />
</html:select>
</td> </tr></tbody> </table>
<c:choose> <c:when test="${applicationVO.nationalityId == '222' }">
<script type="text/javascript">change('NATIONALITY','222','pobStateTbl','pobStateIntTbl','');</script>
</c:when> <c:otherwise>
<script type="text/javascript">change('NATIONALITY','221','pobStateTbl','pobStateIntTbl','');</script>
</c:otherwise> </c:choose>
<form name="pobForm">
<input type="hidden" name="pob3" value="">
</form>
[/html]
THE PROBLEM(s)
1. Only 1st table (id=pobStateIntTbl) is sent to action.java when submit();
2. Javascript seems don't send value to pobForm.pob3.value (hidden field) and it is declare using html not jstl.
Thank you..
-noIdea-
got it..
somehow,
document.pobForms.pob3.value = val;
did not sent val to pob3.value
then i removed pobForms and replaced with forms[0]
document.forms[0].pob3.value = val;
and then the magic begins..I just don't understand why javascript didn't recognise my pobForms previously. Can someone explain?
somehow,
document.pobForms.pob3.value = val;
did not sent val to pob3.value
then i removed pobForms and replaced with forms[0]
document.forms[0].pob3.value = val;
and then the magic begins..I just don't understand why javascript didn't recognise my pobForms previously. Can someone explain?
-noIdea-
•
•
•
•
hi venkat
i have a similar issue.
i am calling a javacript from a java code and i need to return a value from the javascript file to the calling java file.
kindly share your suggestions with me
thanks
First, you must understand that Javascript is running on client side i.e. At your browser. Meanwhile Java code is running on your server side i.e. At your server.
So, it means that Java code can send value to javascript at the first time it rendered by browser.
Example:
<script>var javascriptValue = <%=valueFromYourJavaCode%></script>
But Javascript can't send back to Java code as it is running only at browser.
<script>
<%String javaValue = %> valueFromYourJavascriptCode <%;%>
</script>
tq.
-noIdea-
![]() |
Other Threads in the JSP Forum
- Previous Thread: JSTL - Obtaining values from url string
- Next Thread: Whats Wrongin folloowind code for connecting with mysql
Views: 37620 | Replies: 14
| Thread Tools | Search this Thread |
Tag cloud for JSP
apache array backbutton combobox comma connection csv database development directorystructure dropdownlist dynamicpagetitles eclipse frames glassfish ie8 imagetodatabse imageupload integer internet java javaee javascript jsf jsp jsppagetitles levels mvc2 mvcmodel2 mysql netbeans network parameters passing ping printinserverinsteadofclient project read redirect request.getparameter response seperated servlet servletdopost()readxml sessions software sql ssl state_saving_method stocks sun tomcat tutorial update values video web write





