I am creating a web interface in jsp. I have a java String variable (let's call it 'a') and some radio buttons, and I am trying to get the value of the checked one on click (without submiting the form) and give it's value to the variable.
I made a research on weather this can be done using only jsp and I could not find anything. So I assumed that I have to do this using Javascript. I am new in Javascript(so please excuse me if my question is stupid), but I wrote the following code.
HTML:
`<input type="radio" name="phase" value="value1" class="checkboxes" id="design_phase" onclick="getRadioValue(this.id)" />Value1
<input type="radio" name="phase" value="value2" class="checkboxes" id="development_phase" onclick="getRadioValue(this.id)" checked/>Value2`
Javascript:
`function getRadioValue(id) {
var radioBtn = document.getElementById(id);
if(radioBtn.value=="value1"){
alert(radioBtn.value);
<%a="value1";
System.out.println("value1!");%>
}
else{
alert(radioBtn.value);
<%a="value2";
System.out.println("value2!");%>
}
}`
When I run this, both System.out contents are printed just when I load the page, before I even choose a checkbox. I have put the alert functions in order to make sure that javascript reads the values correctly, and it does indeed! So the problem seems to be with using js together with jsp.
Does anyone know what I am doing wrong, and how can this be done correctly?
Thanks in advance!