riztak riztak - 7 months ago 7
Javascript Question

how can I reference a select element?

I have 3 selection fields as following:


  • selection field 1

  • selection field 2

  • selection field 3



The selection field 1 and 2 are filled up through a simple MySql query. What I have to do is when user has selected one or several options from selection field 1 and 2, I need to perform an AJAX request to
page2.php
and then they will be returned some results. Fortunately this works well.

In
page2.php
there is a selection field with id
select_field
. The problem appears when I'm trying to reference this id from
page1.php
.

How can I resolve this problem?

Answer

Use event delegation for binding events to dynamically added elements.

$(document).on('click', '#select_field', function(){
    //your code
})
Comments