hi
i have radio button in jsp
i want check that radio is selected or not!!

<input type="radio" name="code1" value="1" >

how can i check in input that radio is selected or not!!
for example put if in input

<input type="radio" name="code1" value="1"  <% if(?)%>>

Recommended Answers

All 4 Replies

Chaeck this site:
http://www.w3schools.com/default.asp
Find the attributes of the radio button at the html tutorials or the html references where all the tags and their attributes are listed.

boolean isCkecked = ....;
<input type="radio" name="code1" value="1"  <%= (isCkecked )?"checked":"" %> />

I am almost certain the "checked" is the right thing to use but better check the site as well

tank you for your reply.
i test that but in both case (selected radio and unselected), it return false value.
i put this

<% 

boolean isCkecked = false;
%>
1<input type="radio" name="code1" value="1"  <%= (isCkecked )?"checked":"" %>   >

<%

session.setAttribute("isCkecked",isCkecked); %>

but anyway it return false !!! :(

i need some like this

<% 
boolean isCkecked = false;
%>
1<input type="radio" name="code1" value="1"   onclick="<%isCkecked = true;%>"   >
<%session.setAttribute("isCkecked",isCkecked); %>

Of course it returns false, since you do this:

<% 
boolean isCkecked = false;
%>
.....

<%
session.setAttribute("isCkecked",isCkecked); 
%>

The isCkecked is declared false. Then you pass its value which is false at the session.

This:

<input type="radio" name="code1" value="1"   onclick="<%isCkecked = true;%>"

Will not do what you think it does. Every time you run it the value of isCkecked will ALWAYS BE TRUE, because this: isCkecked = true; is executed before the page loads. It doesn't display anything at the page.

When it loads the isCkecked is already true and nothing can change its value. When the page loads the html code that will be rendered is this:

<input type="radio" name="code1" value="1"   onclick=""

This: <%isCkecked = true;%> just executes this code: isCkecked = true; before the page loads

Whenever you click the radio button you CANNOT change the value of any java variable. The java code is executed at the server and javascript is executed at the client. The onlclick executes javascript code and whatever you write there doesn't change any java values.

What you need is to submit a form with the radio button, take the value with request.getParameter, do whatever you want and then redirect back to the page.

This:

<% 
boolean isCkecked = true;
%>

<input type="radio" name="code1" value="1" <%=(isCkecked)?"checked":"" %>   >

First the isCkecked becomes true.
Then this is executed: (isCkecked)?"checked":"" and the result is the String checked.
Then the page loads like this: <input type="radio" name="code1" value="1" [B]checked[/B] > So in the end the html code that you see at the page is this:

<input type="radio" name="code1" value="1" checked />

So how many times you click the radio button nothing will happen.

This: <%= (...) %> makes the result be displayed but the result is calculated BEFORE the page loads. When it does is just simple html text. (checked )

So TnX for your advantageous guides.
it seems that i go wrong way!!:icon_wink:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.