0

I am working in my website http://openacessjournal.com One thing is that the first input field is static after that second and third input field is created by jQuery, When I am trying to using onchange() function, the function is working for only first static input fields, not for all.

My code is as below:

$(document).ready(function() {
  $("select[name='author_number[]']").each(function() {
    $(this).change(function() {

      alert($(this).val());

      var selected = $("option:selected", $(this)).val();
      var thisID = $(this).prop("id");
      $("select[name='author_number[]'] option").each(function() {
        $(this).prop("disabled", false);
      });

      $("select[name='author_number[]']").each(function() {
        if ($(this).prop("id") == thisID) {
          $("option[value='" + selected + "']", $(this)).prop("disabled", true);
        }
      });

    });
  });
});
Dharman
  • 30,962
  • 25
  • 85
  • 135

1 Answers1

0

If you're dynamically creating 2 input (or more) fields, they need to be binded since they were not in the DOM on initial page load ($(document).ready()).

$(document).ready(function() {
    $('body').on('change', 'select[name="author_number[]"]', function() {
        alert($(this).val());

        var selected = $("option:selected", $(this)).val();
        var thisID = $(this).prop("id");

        $("select[name='author_number[]'] option").each(function() {
            $(this).prop("disabled", false);
        });

        $("select[name='author_number[]']").each(function() {
            if ($(this).prop("id") == thisID) {
                $("option[value='" + selected + "']", $(this)).prop("disabled", true);
            }
        });
    });
});
N'Bayramberdiyev
  • 5,936
  • 7
  • 27
  • 47