Alimon Karim
Alimon Karim

Reputation: 4469

Cakephp get SELECT's value and text by jquery

I have tried to get value from select option in cakephp,Here is my code

 echo $this->Form->input('division', array('type' => 'select',
                                           id=>'my-select',
                                          'options' =>   array('Dhaka' => 'Dhaka', 'Dinajpur' => 'Dinajpur','Others'=>'Others'),'selected' => 'Dhaka'));

Here is my js code

   <script>
    $(document).ready(function(){
    $("#my-select").change(function() {
        alert($('#my-select option:selected').html());
    });
});
</script>

Problem is no any feed back given.I am enable to find error.Where is the problem ?

Upvotes: 0

Views: 1412

Answers (2)

Gara
Gara

Reputation: 626

$(document).ready(function(){
  $("#my-select").on('change',function(e){
     var select_value = $(e.currentTarget).val();
     alert(select_value);
  });
});

Upvotes: 0

Sudharsan S
Sudharsan S

Reputation: 15403

Use .on() in jquery. Cakephp rendered dom elements dynamically.

$(document).on("change" , "#my-select" , function() {
    alert($('#my-select option:selected').html());
});

Upvotes: 2

Related Questions