Getting details of the elements by using its Name.
document.getElementsByName('f_name')
Here f_name is the name attribute of a textbox.
Output here
Here is the code for getting data entered inside a textbox by using value
<input type=text name=f_name >
<input type=button value=Submit onClick=my_check()>
<br>
<div id=d1_text>Output here </div>
<script type="text/javascript">
function my_check(){
var my_data = document.getElementsByName('f_name'); // array
document.getElementById('d1_text').innerHTML=my_data[0].value
}
</script>
In above code my_data is an array and its first element is my_data[0]. If we have more number of elements with same name then we can get all values by using my_data as an array.
length
my_data.length : will give total number of elements using the same name.
Radio button using getElementsByName
my_r1
my_r2
my_r3
Output here
One group of radio buttons will have one common name. Here is the code to get selected radio button value by using getElementByName
<input type=radio name=r1 value=my_r1> my_r1
<input type=radio name=r1 value=my_r2> my_r2
<input type=radio name=r1 value=my_r3> my_r3
<input type=button name=b1 id=b1 value=Submit onClick=my_check()>
<br>
<div id=d1>Output here </div>
<script type="text/javascript">
function my_check(){
var my_data = document.getElementsByName('r1'); // array
flag=0
for(i=0;i<my_data.length;i++){
if(my_data[i].checked){
var str=my_data[i].value
flag=1
}
}
if(flag==1){
document.getElementById('d1').innerHTML=str
}else{
document.getElementById('d1').innerHTML=" Radio button is NOT Selected"
}
}
</script>
Checkbox using getElementsByName
All checkboxes will have common name.
Python
PHP
JavaScript