Talha Habib Talha Habib - 6 months ago 9
jQuery Question

trigger event on selected option

Is it possible to trigger an event on selected option without submitting form?

<h3 id="center" align="center">OUR SITE CAN ONLY PROVIDE YOU INFO ABOUT MANTIONED COUNTRIES</h3>

<SELECT style="padding:10px;margin-left: 550px;margin-top: 20px;">
<option name="india">India</OPTION>
<option name="pakistan">Pakistan</OPTION>
<option name="US">UNITED STATE</OPTION>
<option name="UK">UNITED KINGDOM</option>
</SELECT>

Answer

You can't do that with PHP (since it's server side language). Use js/jQuery for that.

$(document).ready(function() {
  $("#dropdown").change(function() {
    alert("Selection: " + $("option:selected", this).val() + ":" + $("option:selected", this).text());
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h3 id="center" align="center">OUR SITE CAN ONLY PROVIDE YOU INFO ABOUT MANTIONED COUNTRIES</h3>

<select id="dropdown" name="countries" style="padding:10px;margin-left: 550px;margin-top: 20px;">
  <option value="india">India</option>
  <option value="pakistan">Pakistan</option>
  <option value="US">UNITED STATE</option>
  <option value="UK">UNITED KINGDOM</option>
</select>

Also, name attribute is for select not for option. Check snippet markup changed.

Comments