Javascript Get Radio Button Value

Javascript Get Radio Button Value mytechlearns
JavaScriptLeave a Comment on Javascript Get Radio Button Value

Javascript Get Radio Button Value

Javascript Get Radio Button Value

Every software need Radio button in many place like gender, department, religion and so many place. Javascript Radio button use for collect those information from the user only check box. Let’s discuss index.php page for Get Radio Button Value using Javascript:

<!DOCTYPE html> 
<html> 

<head> 
	<title> 
		Get value of selected radio button 
	</title> 
</head> 

<body> 
	
	Gender: 
	<input type="radio" name="gender" value="Male">Male 
	<input type="radio" name="gender" value="Female">Female 
	<input type="radio" name="gender" value="Others">Others 
	
	<br> 
	
	<button type="button" onclick="displayRadioValue()"> 
		Submit 
	</button> 
	
	<br> 
	
	<div id="result"></div> 
	
	<script> 
		function displayRadioValue() { 
			var ele = document.getElementsByName('gender'); 
			
			for(i = 0; i < ele.length; i++) { 
				if(ele[i].checked) 
				document.getElementById("result").innerHTML
						= "Gender: "+ele[i].value; 
			} 
		} 
	</script> 
</body> 
</html>					 

In here, radio button get value by using Javascript name tag document.getElementsByName(”)[].checked

That’s all for today. Happy Coding…

Source Code: Click Here

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top