CodeIgniter Forums
using PHP id in Javascript - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: using PHP id in Javascript (/showthread.php?tid=54025)



using PHP id in Javascript - El Forum - 08-19-2012

[eluser]Byzs[/eluser]
Hello everybody, asking for a little help here, I'am trying to change an <option> value from Javascript using the id of the <select> which is dynamically generated by PHP.

this is my PHP code:

Code:
$modOr[0]="HCS";
$modOr[1]="CN";
$modOr[2]="CAD";
$modOr[3]="FG";
$modOr[4]="FOCSH";
$modOr[5]="FOCN";
$modOr[6]="FOAM";

for($m=0; $m<7; $m++)
{                                    
       echo("<select class='element select' id='curr".$modOr[$m]."'>");
}

and this is how I get access from the Javascript code:
Code:
document.getElementById("formName").currHCS.value = 1;


I have others <select> tags in the forms and they works fine when I try to access them from Javascript code...but their id is not dynamically generated, its just HTML.

I'am really stuck here, I don't if this is some CI problem or my code is wrong.
I added the id forum in the form_open like this:

Code:
$attributes = array('id' => 'formName');
echo form_open('',$attributes);

should I add the <select> id too?

Thanks, and sorry for the bad English ! Smile


using PHP id in Javascript - El Forum - 08-21-2012

[eluser]deed02392[/eluser]
You're creating multiple SELECT elements in this code, not one select element with <option>'s, which is what I think you are supposed to be doing.

You need to read about what the HTML for a proper SELECT looks like.

Then read about accessing options in a SELECT from JavaScript (preferably with a library, like jQuery).

This should give you enough knowledge to achieve what you want.


using PHP id in Javascript - El Forum - 08-21-2012

[eluser]Byzs[/eluser]
[quote author="deed02392" date="1345537616"]You're creating multiple SELECT elements in this code, not one select element with <option>'s, which is what I think you are supposed to be doing.

You need to read about what the HTML for a proper SELECT looks like.

Then read about accessing options in a SELECT from JavaScript (preferably with a library, like jQuery).

This should give you enough knowledge to achieve what you want.[/quote]

Thanks for the reply deed02392, yes I'am creating multiple's <select> to reduce the information per <select> I have.

What I did not include in this code (ouch) is that I'am creating multiple <options> per every <select>....anyway what I'am trying to archive is to select a specific option of a <select> throw an other <select> with Javascript.

Here it is the complete code:

Code:
$modOr[0]="HCS";
$modOr[1]="CN";
$modOr[2]="CAD";
$modOr[3]="FG";
$modOr[4]="FOCSH";
$modOr[5]="FOCN";
$modOr[6]="FOAM";

for($m=0; $m<7; $m++)
{
if($m==0)
    echo("<div id='div".$modOr[$m]."' ");
else
    echo("<div id='div".$modOr[$m]."' ");

echo("<select id='curr".$modOr[$m]."'>");

        foreach ($curr->result_array() as $cur)
        {
            if($cur["databaseTable"]==$modOr[$m]){
                echo("<option value='".$cur['databaseValue']."'>".$cur['databaseValue']."</option>");
            }
        }
echo("</select>");
echo("</div>");
}

Sorry for the mistake

PD: on line 12 there is a "style = 'display : block'" and for line 14 the same but with display "none"...that for some reason is not appearing, thats why I'am using the "if($m==0)"