0

I am trying to create a simple javascript function which will populate 2 dropdown boxes, one for age and the other for gender.

However,the functions are not working and i cant seem to figure out why.Any help would be appreciated.

Code:

    <head>

<script type="text/javascript">

    function AgeDropDown(){
        var list=getElementById(UserProfileAge);
        for(var i=1;i<100;i++)
        {
            var opt = document.createElement("option");
            opt.value= i;
            UserProfileAge.appendChild(opt);
        }
    }

    function genderlist(){
        var choices=new array["M","F"];
        for(i=0;i<choices.length;i++)
        {
            var opt = document.createElement("option");
            opt.value= i;
            UserProfileGender.appendChild(opt);
        }
    }

</script>

</head>
<body>
<?php
include("usermenubar.inc");
?>
<form id='UserProfile' name='UserProfile' method='POST' action='editdetails.php'>


<div class='UserDetails'><!--dropdown-->
    Age:<select id='UserProfileAge' name='UserProfileAge' onclick='AgeDropDown'>
    <option value=''>Please enter your age</option>
    </select>
</div>

<div class='UserDetails'><!--Dropdown-->
    Gender:<select id='UserProfileGender' name='UserProfileGender' onclick="genderlist">
    <option value=''>Please enter your gender</option>
    </select>
</div>


<input type='submit' name='UserProfleSubmit' value='Save Changes'/>
</form>
</body>
</html>
1
  • 2
    try with AgeDropDown() and genderlist() in html "onclick" Commented Oct 20, 2013 at 11:04

2 Answers 2

1

This can help:

function load(){
    AgeDropDown();
    genderlist();
    }

 function AgeDropDown(){
        var list=document.getElementById("UserProfileAge");
        for(var i=1;i<100;i++)
        {
            var opt = document.createElement("option");
            opt.value= i;
            list.appendChild(opt);
        }
  }

    function genderlist(){
        var choices=new array["M","F"];
        for(i=0;i<choices.length;i++)
        {
            createOption(document.getElementById("UserProfileGender"));
        }
    }
Sign up to request clarification or add additional context in comments.

2 Comments

I've tried what you've listed, but am still getting the an Uncaught ReferenceError:getElementById is not defined. Upon clicking it, it points to AgeDropDown,load and onload. i have no idea why its being undefined when it already is..
The first line inside AgeDropDown() should be var list=document.getElementById("UserProfileAge");
0

Your functions are loading, but they are never executed because they are never called. You have to call the function at some point in your html file. The best practices it to run initialization code like on onload event of the page.

<script>
function load()
{
  AgeDropDown();
  genderlist()
}
... 
</script>



<body onload="load()">

4 Comments

HI Alon, Thanks for taking the time to answer, i've edited my code with your suggestions. HOwever, upon loading the page and clicking on the selects i am getting an uncaughtreferenceerror for the onclicks. Any ideas why this is so?
var list=getElementById(UserProfileAge); should be var list=getElementById("UserProfileAge");
@ken please don't do that - don't overwrite your question with a new one. Even appending a new question to another one is bad practice.
See the comment from Ritesh. He is right. Also, remove the onclick from both the <select> tags. You don't need it there. The data has already been loaded in the "load" function Also use document.getElementById instead of just getElementById

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.