Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6755879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:27:20+00:00 2026-05-26T13:27:20+00:00

i am trying to create a select element with JS or even edit an

  • 0

i am trying to create a select element with JS or even edit an existing one yet i seem to be missing something.
this is done in Joomla if this matters.

this is my code:

 var option = document.createElement("option");
var select = document.createElement("select");
 select.setAttribute("id", "chooseCat");

for(int i=0;i<LevelNames.Length;i++)
    {
      option.innerHTML = LevelNames[i];
      option.setAttribute("value",LevelIds[i]);
      document.getElementById("cat_chooser").appendChild(option);
      document.getElementById("cat_chooser").options.add(option);
    }

select.onchange=function()
{
  CreateDDL(this.options[this.selectedIndex].value);

}

var test = document.getElementById("cat_chooser");
test.appendChild(select);
document.add(select);
document.appendChild(select);

this is all the ways i tried doing that.
cat_chooser is a SELECT added manualy to the page.

any help?

EDIT:
this is the whole code :

      <script language=\"javascript\" type=\"text/javascript\">


    //definitions
    var LevelNames = new Array();
    var LevelIds = new Array();
    boolean isFirstRun = true;


    //this functions create a Drop Down List 
    function CreateDDL(pid=null){

    //pass arrays for client side, henceforth : var id,var parent_it, var title
    <?php echo "\n".$id."\n".$parent_id."\n".$title."\n\n";?>
    if(pid){

    }
     if(isFirstRun)
        {
    for(int i=0; i < id.length;i++)
            {
    //if category has no parent
       if(parent_id[i] == "1")


            {
                LevelIds.push(id[i]);
                LevelNames.push(title[i]);

                }    
            }
        }
    else{
    for(int i=0; i < id.length;i++)
            {

    //if is a son of our target?
       if(parent_id[i] == pid)
            {
            LevelIds.push(id[i]);
            LevelNames.push(title[i]);

            }    
        }

}
//finished first run
isFirstRun=false;

//create the actuall drop down
//var option = document.createElement("option");
var select = document.createElement("select");
 select.setAttribute("id", "chooseCat");
for(var i=0;i<LevelNames.length;i++)
    {
       var option = new Option(/* Label */ LevelNames[i],
                              /* Value */ LevelIds[i]   );
      select.options.add(option);
    }

    select.onchange=function()
    {
      CreateDDL(this.options[this.selectedIndex].value);

    }
    var test = document.getElementById("cat_chooser");
    test.appendChild(select);
    //document.add(select);
    //document.appendChild(select);
    document.body.appendChild(select);

}
CreateDDL();
</script>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T13:27:20+00:00Added an answer on May 26, 2026 at 1:27 pm
    • JavaScript is not Java. You cannot use int or boolean to declare variables. Instead, use var.
    • JavaScript is not PHP. You cannot define a default value using function createDDL(pid=null)
    • The .add method is only defined at the HTMLSelectElement.options object.
    • .appendChild should be used on document.body, not document, because you want to add elemetns to the body, rather than the document.

    Working code, provided that <?php .. ?> returns valid JavaScript objects.

    <script language="javascript" type="text/javascript"> //No backslashes..
    //definitions
    var LevelNames = new Array();
    var LevelIds = new Array();
    var isFirstRun = true;
    
    //this functions create a Drop Down List 
    function CreateDDL(pid) {
        if(typeof pid == "undefined") pid = null; //Default value
        //pass arrays for client side, henceforth : var id,var parent_it, var title
        <?php echo "\n".$id."\n".$parent_id."\n".$title."\n\n"; ?>
        if (pid) {
    
        }
        if (isFirstRun) {
            for (var i = 0; i < id.length; i++) {
                //if category has no parent
                if (parent_id[i] == "1")
    
                {
                    LevelIds.push(id[i]);
                    LevelNames.push(title[i]);
    
                }
            }
        } else {
            for (var i = 0; i < id.length; i++) {
    
                //if is a son of our target?
                if (parent_id[i] == pid) {
                    LevelIds.push(id[i]);
                    LevelNames.push(title[i]);
    
                }
            }
    
        }
        //finished first run
        isFirstRun = false;
    
        //create the actuall drop down
        //var option = document.createElement("option");
        var select = document.createElement("select");
        select.setAttribute("id", "chooseCat");
        for (var i = 0; i < LevelNames.length; i++) {
            var option = new Option(/* Label */ LevelNames[i],
                                    /* Value */ LevelIds[i]);
            select.options.add(option);
        }
    
        select.onchange = function () {
            CreateDDL(this.options[this.selectedIndex].value);
    
        }
        var test = document.getElementById("cat_chooser");
        test.appendChild(select);
        //document.add(select);
        //document.appendChild(select);
        document.body.appendChild(select);
    
    }
    CreateDDL();
    </script>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create an onchange event using javascript on a select element. But
I'm trying to create a column-based list within a <select> element with multiple=multiple -
Trying to create Database as follows: USE Master GO IF NOT EXISTS(SELECT [Name] FROM
I am trying to create a view for a UNION of 2 select statements
I'm trying to create an element that contains a form so it can be
I am trying a create a facility (using jquery) to be able to select
I'm trying to create an outlook style To: / recipients list when I select
I want to create a JavaScript/jQuery/jQuery UI drop-down select for element ordering in my
I'm trying to pick a random element from an array -- is this possible
I'm trying to create a page where a user selects the type of subscription

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.