John
John

Reputation: 4028

Select element by name

<div class="fes-fields">
  <select name="departure_country[]" data-required="1" data-type="select">
    <option value="">- Chọn nước -</option>
    <option value="Mỹ">Mỹ</option>
    <option value="Úc">Úc</option>
    <option value="Pháp">Pháp</option>
  </select>
</div>

The jQuery script to print log is

//Function executes on change of first select option field 
jQuery('[name="departure_country[]"]').change(function () {
    console.log("Selected");
}

The console.log does not print anything.

Upvotes: 0

Views: 53

Answers (2)

KAD
KAD

Reputation: 11102

You have a brackets problem ); is missing:

//Function executes on change of first select option field 
jQuery('[name="departure_country[]"]').change(function () {
    console.log("Selected");
    $("#txt").html("Selected");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="fes-fields">
    <select name="departure_country[]" data-required="1" data-type="select">
        <option value="">- Chọn nước -</option>
        <option value="Mỹ">Mỹ</option>                          
        <option value="Úc">Úc</option>                      
        <option value="Pháp">Pháp</option>          
    </select>
</div>
   <div id="txt"></div>

Upvotes: 1

Milind Anantwar
Milind Anantwar

Reputation: 82231

You need to ensure that select element is loaded when you are trying to attach event to it. If the contents are static then wrap the event in document ready function

also make sure that you are closing the missing bracket ) after change event:

$(function(){
 jQuery('[name="departure_country[]"]').change(function () {
  console.log("Selected");
 });
});

Upvotes: 1

Related Questions