0

I have a gender field in my form.

I have given it the same name, i.e. 'gender', and I have given it the same id, i.e. 'gender'.

Now I want to show what I have selected out of the two options. But with same id, it is not working. I have applied the onclick event to a textbox and I want it so whenever I click on that textbox, I show a javascript alert showing what I have selected, either male or female.

Please help!

gender:<input type="radio" name="gender" id="gender" value="female"/>female
      :<input type="radio"  name="gender" id="gender" value="male" />male

  <input type="text" name="textbox" onclick="check()" />



   <script type="text/javascript">
      function check()
 {

 var a=document.getElementById("gender").value;
 alert(a);
 }

</script>

     function addUser() {
//how to check what is the selected radio input
alert(getCheckedRadioId('u_type'));
    }
         function getCheckedRadioId(name) {
var elements = document.getElementsByName(name);

for (var i=0, len=elements.length; i<len; ++i)
    if (elements[i].checked) return elements[i].value;
  }


 </script>

  now this code alerts that what i have choosen but the problem is that i want the    alert's value  in a variable so that i could send this value to database...so how should i take the alerted value in a variable.....
1
  • WElcome to SO. Next time, please end your sentences with just 1 . , not 3 or 4. Keep in mind to try and write your question so its easier for the people here to dive into your problem! Commented Dec 17, 2013 at 7:50

2 Answers 2

2

You might wanna have a look at this: javascript selected radio

Basically if you had a function that gets the checked radio value by name the problem would be solved. Adopted from the link above:

var selected = '';
var elements = document.getElementsByName('gender');

for (var i = 0, i < elements.length; i++)
{
    if (elements[i].checked)
        selected = elements[i].value;
}

alert(selected);

Also you might consider using jQuery. It would help a lot in cases like this one.

Sign up to request clarification or add additional context in comments.

2 Comments

thankyou so much for the help...its working absolutely fine...thanxx a lot
You might want to check IF the user has selected anything. If he didn't the selected value will be an empty string in this case. You also might wanna mark this answer as accepted so others would know it works.
0

Your problem is that each element must have a unique Id, and you are giving two elements the same Id. I am not sure which one getElementById() would return. I assume the first.

However, you are quite ok giving multiple elements the same name. Then you could use getElementsByName() to retrieve an array containing the two elements, and you could return whether the element's Checked property is set.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.