HI,

This is my first time posting.
I have a php function with opgroup and option (category and subcategory)

PHP Code:
function genereazahtmlforselect($rows$valsubcategory ''$valcategorie '')
{
   
$html '<select name="subcategori" id="categorii" class="cascading" title="Categorie&nbsp;">&nbsp;';
   
$html .= '<optgroup label="Selecteaza">';
   
$html .= '<option value="">Alege o Categorie</option></optgroup>';
   
       foreach (
$rows as $category) {
          if(
$category['parinte'] == '0'){
              
$html .= '<optgroup id="' $category['id'] . '" title="&nbsp;Subcategorie&nbsp;" label="' $category['name'] . '">&nbsp;';
              foreach (
$rows as $subcategory) {
                if (
$subcategory['parinte'] == $category['id']){
               
                  
$html .= '<option value="'.$subcategory['id'].'"';
              if (
$subcategory['id'] === $valsubcategory) {
                
$html .= 'selected = "selected"';
              }
              
$html .= '>'.$subcategory['name'].'</option>';
                }
              }
              
$html .= '</optgroup>';
}
        }
    
$html .= '</select>';
   return 
$html;

After that I use the javascript function splits into two select..

Code JavaScript:
extractMasterMenu: function(menu)
  {
    var masterMenu = document.createElement("select");
    masterMenu.id = menu.id + "master";
    masterMenu.setAttribute("name", masterMenu.id);
    masterMenu.setAttribute("title", menu.getAttribute("title"));
    masterMenu._slave = menu;
 
    while (menu.hasChildNodes())
    {
      var optgroup = menu.firstChild;
      if (optgroup.nodeType == 1)
      {
        var masterOption = document.createElement("option");
      masterOption.setAttribute("id", optgroup.getAttribute("id"));
      masterOption.setAttribute("value", optgroup.getAttribute("id"));
        masterOption.appendChild(document.createTextNode(optgroup.getAttribute("label")));
        masterMenu.appendChild(masterOption);
 
        var slaveOptions = [];
        while (optgroup.hasChildNodes())
        {
          var option = optgroup.firstChild;
          slaveOptions[slaveOptions.length] = option;
          optgroup.removeChild(option);
        }
        masterOption._slaveOptions = slaveOptions;
 
        menu.setAttribute("title", optgroup.getAttribute("title"));
      }
      menu.removeChild(optgroup);
    }
 
    menu.parentNode.insertBefore(masterMenu, menu);
 
    return masterMenu;
  }

So I tried to bring selected = selected for this javascript code generated by optgroup.
PHP Code:
if (isset( $categori )) {
?>
<script type="text/javascript">
(function () {
   
    var categorieSelectata = <?php echo "'$categori'"?>;
    var categorii = document.getElementById("categoriimaster");
   
    alert(categorii);
   
    for ( var i = 1; i < categorii.length; i++ ) {
       
        if ( categorii.options[i].value === categorieSelectata ) {
           
            categorii.selectedIndex = i;
            break;
        }
    }
})();
</script>
<?php
}
categorieselectata return a number and its ok
Alert return me null..If someone can help me to keep the name selected after submit..
Thanks for reading!