Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to set dynamic values from database to radio buttons in a JSP page??

843840Apr 22 2008 — edited Apr 25 2008
i am trying to set values(checked) from database to radio button tags. Depending on the values from the backend the radio buttons in JSP page to be checked. I am not using any frameworks(no struts or JSF), its just simple JSP page where i am trying to display data. Using <jsp:usebean /> to load the bean where i have delecared my String[] property set/get methods & method to reurn the values from database.

Eg : I have a String[] which has 10 values(y,y,y,y,y,y,y,y,,) which is are values reterived from database depending on my condition.

Now i have 10 sets of radio buttons(3 each) in my JSP page

<input type="radio" name="name1" value="Y">
<input type="radio" name="name1" value="N">
<input type="radio" name="name1" value="UN">

<input type="radio" name="name2" value="Y">
<input type="radio" name="name2" value="N">
<input type="radio" name="name2" value="UN">

etc 10 sets..

depending on the values from database, if the first value in the String [] is 'Y' then first radio button should be checked..same for all the values..

I am trying to set it using Javascript but nothing is happening??

Can any one suggest on this??

code:

<script type="text/javascript">

var namesArray = new Array();
for(var i=1;i<=10;i++){
namesArray[i] = "risk"+i;
}

document.write("Names Array : "+namesArray);

var valuesArray = new Array();
<%
for(int j=0;j<status.length;j++) {
%>
valuesArray[<%=j%>] = "<%=status[j]%>";

<%
}
%>
document.writeln(" Values Array : "+valuesArray);

function loadCheckBox() {
for(i=1;i<namesArray.length;i++) {
var elementName = namesArray;
var elementArray = document.getElementsByName(elementName);
for(j = 0; j<elementArray.length; j++) {
if(elementArray[j].value == valuesArray[i]) {
if(valuesArray[i].value == 'Y') elementArray[j].checked = "checked"
else
elementArray[j].checked = ""
}
}
}
}
</script>
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 23 2008
Added on Apr 22 2008
4 comments
1,672 views