I want to use javascript to check for the radio button whether is checked or not.
If the tidak setuju radio button is checked or setuju radio button is not checked, I want to alert a dialog box.
But somehow the onclick function at the submit button is not working.
I temporarily uploaded my page to a free server.
Here is my site, and this is the external javascript file.
What is wrong with my code?
In my HTML, the code for the radio button and the submit button :
<tr>
<td width = "25%">
<div align="right">
<input type="radio" name="RadioGroup1" value="Setuju" id="RadioGroup1_0" />
</div></td>
<td width = "25%" class = "left">Setuju</td>
<td width = "25%" class = "right"><div align="right">
<input type="radio" name="RadioGroup1" value="Tidak Setuju" id="RadioGroup1_1" />
</div></td>
<td width = "25%" class = "left">Tidak Setuju</td>
</tr>
</table>
<input id="Submit" name="Submit" type="submit" value="Submit" class="Button" onclick="return agree(); " />
And here is the javascript :
function agree(){
if(document.getElementById("RadioGroup1_1").checked || !document.getElementById("RadioGroup1_0").checked)
{
alert("Anda mesti setuju syarat-syarat permohonan.");
return false;
}
}
Correction :
I forget to set ID for my other textfield where there is more conditions in the function agree(). Hence, the function did not run properly.
Here is my error :
<input type="text" name="stafNumber" /> <-- missing ID declaration, but declared in javascript.
On line 19 of checkform.js you have an error in your validation function:
This gives “Uncaught TypeError: Cannot read property ‘value’ of null” because there is no such element with ID “stafAddress” on your page.
There is also another error that you will need to fix on line 13:
Gives a similar error for a similar reason.
Have you tried Chrome’s Javascript debugger? It’s very useful for working out what’s wrong. You can open it at Menu > Tools > Developer Tools > Sources.