Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <ul> <li>JavaScript is <strong>not</strong> Java. You cannot use <code>int</code> or <code>boolean</code> to declare variables. Instead, use <code>var</code>.</li> <li>JavaScript is <strong>not</strong> PHP. You cannot define a default value using <code>function createDDL(pid=null)</code></li> <li>The <code>.add</code> method is only defined at the <code>HTMLSelectElement.options</code> object.</li> <li><code>.appendChild</code> should be used on <code>document.body</code>, <em>not</em> <code>document</code>, because you want to add elemetns to the body, rather than the document.</li> </ul> <p>Working code, provided that <code>&lt;?php .. ?&gt;</code> returns valid JavaScript objects.</p> <pre><code>&lt;script language="javascript" type="text/javascript"&gt; //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 &lt;?php echo "\n".$id."\n".$parent_id."\n".$title."\n\n"; ?&gt; if (pid) { } if (isFirstRun) { for (var i = 0; i &lt; 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 &lt; 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 &lt; 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(); &lt;/script&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload