0

I am working on HTML and Javascript.

I have a label which you can see below

<label for="optGender">Gender: </label> 
<input id="optGender" type="radio" name="optGender" value="F" onclick="changeGender(this.value)">
Female
<br>
<input id="optGender" type="radio" name="optGender" value="M" onclick="changeGender(this.value)">
Male
<br>
Where do you want to live?
<br>
<select id="txtCity" name="lstCity" size="5">
<option value="06">Ankara</option>
<option value="34">Istanbul</option>
<option value="35">Izmir</option>
<option value="27">Gaziantep</option>
</select>
<br>

and in my javascript file I have this function to check whether its checked or not

function changeGender(whichOne){

    var gender = document.forms["ProcessInfo"]["optGender"].value;
    var optGender = document.getElementById("optGender");
    var txtCity = document.getElementById("txtCity");

    if ( !document.getElementById('optGender').checked )
    {
    errMessage += "Please select your gender.\n";
    document.getElementById('optGender').checked = false;
    }
    //if ( gender == "M" ) { alert("lol");}

}

When I select "Female" button, it works, doesn't give an error. But when I choose male, it gives me an error that I havent selected any option. What is wrong with that?

Edit * What I actually want to do is, If I select F the list will have the cities which starts with I, for M its going to be G. I can easily handle this part, but I'm stuck at easiest part.

1
  • Provide jsFiddle examle here. Commented Nov 7, 2013 at 1:45

3 Answers 3

3

you have the same ID for both inputs. That won't work... Document.getelementbyid returns the first instance it finds the id you provided, which is the male input. I think what you might want here is a select instead of an input...

Give me a second and I'll modify your code for you.

<label for="optGender">Gender: </label> 
<input id="Female" type="radio" name="optGender" value="F" onclick="changeGender(this)">
Female
<br>
<input id="Male" type="radio" name="optGender" value="M" onclick="changeGender(this)">
Male
<br>
Where do you want to live?
<br>
<select id="txtCity" name="lstCity" size="5">
<option value="06">Ankara</option>
<option value="34">Istanbul</option>
<option value="35">Izmir</option>
<option value="27">Gaziantep</option>
</select>
<br>


function changeGender(whichOne){
    var gender = whichOne.value;
    if ( gender == "M" ) { alert("lol");}
    if ( gender == "F" ) { alert("wat");}

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

3 Comments

Edited my html file sir, I will remove options from "txtCity" but I still can't handle the select issue. It works for only female. @deweyredman
Alright...try that out. Let me know if you run into anything.
It works like a charm sir, If both cases == null, it gives an error for both cases now. Thanks! ( And /laughed at ("wat") part :)
0

You need to change your radio's ID and keep the name. It'll work

1 Comment

When I change the ID, it doesn't select the radio button
0

You cannot have 2 elements with the same id in HTML. You have 2 elements with same id ie optGender.

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.