skytorner
skytorner

Reputation: 415

how to retrieve value of a dynamically generated option with jquery

I know that this question was already asked but it seems I dont understand a lot of DOM functionment.

What I'm trying to do is to retrieve the value of a dynamicaly generated with Jquery and CodeIgniter. But when I look the source code, i cant see any element generated.

Here's my html page

    <div id = "panelMarque" class="col-md-4">
        <div class=" panel panel-default">
            <div class="panel-heading text-center">
                Modèle
            </div>

            <div id = "modelesPc"class="panel-body">
                <select id = "selectPC" class="form-control" name = "modelesPC">
                    <!-- =========================FILL BY AJAX REQUEST =================================-->
                </select> 
            </div>
        </div>          
    </div>

Here's my ajax call

$(document).ready(function(){
  $("#marque").change(function()
  {
    $.ajax({
        url : "<?php echo site_url(); ?>/compatibiliteJeu/ajaxModeleByMarque",
        data : {marqueId: $(this).val()},
        type : "POST",
        success : function(data){               
            $("#selectPC").html(data) ; 
            }
        })
  }) ;
}); 

And here's my controller

function ajaxModeleByMarque()
{
    $marqueId = $this->input->post('marqueId') ; 
    $pcByMarque['unPc'] = $this->admin_model->getPcByMarque($marqueId) ; 
    $output = '<option selected="selected">Choisissez un modèle</option>' ; 

    foreach ($pcByMarque['unPc'] as $pc)
    {
        $output .= "<option value ='".$pc->pc_id."' >".$pc->pc_lib."</option>" ; 
    }
    echo $output ; 
}

It works fine. But I have no idea how to retrieve the new value of this generated option. I've already tried .change function but... As I said, when you look at the generated code, theres no 'option' there

Any ideas, solutions and explanation would be great !

Upvotes: 0

Views: 303

Answers (2)

MH2K9
MH2K9

Reputation: 12039

$('#modelesPc').on('change', '#selectPC',function(){
     var value = $(this).val();
})

Upvotes: 1

ComputerLocus
ComputerLocus

Reputation: 3618

Try using the .change() function provided in jquery. This way whenever a change is made to the element you can retrieve the text. If I understood the question correctly you want to get the contents of the field when it gets dynamically changed.

Well change will work for this.

Upvotes: 1

Related Questions