Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery execute function when option is selected from option object

I am dynamically creating the options for a select element using jQuery. Is it possible in jQuery to setup a function to be executed when that option is selected?

I know I can detect the change of the entire select element, but is it possible to specify this on a per-option basis? Maybe something like this:

$('<option />').onselect(function(){

 // do something

});

Edit: If it's not possible to specify a function that get's executed when a specific option is selected, is it possible to bind a function to an element in jQuery? It would make my logic cleaner by allowing me to just simply execute that function assigned to the option in the .change for the select.

like image 877
GoldenNewby Avatar asked Mar 12 '12 04:03

GoldenNewby


People also ask

How do you select an option in a jQuery script?

The jQuery select option syntax in the script tag is below: $ (“selector option: selected”); The jQuery select option is used to display selected content in the option tag. The jQuery select option with. text syntax is below: var variableValue = $ (“selector option: selected”).text ();

How do I get the option that is selected from an option?

Select elements have an options array that has all the options in it, and the select element also has a .selectedIndex property that indicates which option is selected. So, to get the option that is selected you do this: <select onchange=“something ( this )”> where ‘this’ refers to the <select> element. Then, inside the function:

How do you select an option from an array in HTML?

Select elements have an options array that has all the options in it, and the select element also has a .selectedIndex property that indicates which option is selected. So, to get the option that is selected you do this: <select onchange=“something ( this )”> where ‘this’ refers to the <select> element.

What is the “option selected” attribute used for?

The “option: selected” attribute is used to select specific content in the option tag. The selector is the id name (#IdName) of the select tag. The basic syntax used in the web page is below.


3 Answers

You can delegate a change event to the document and attach an event handler to the select element. This allows for runtime manipulation:

$(document).on('change', 'select', function() {
    console.log($(this).val()); // the selected options’s value

    // if you want to do stuff based on the OPTION element:
    var opt = $(this).find('option:selected')[0];
    // use switch or if/else etc.
});

Or if you insist on creating functions for each OPTION, you can do:

$('<option>').data('onselect', function() {
    alert('I’m selected');
});

$(document).on('change', 'select', function(e) {
    var selected = $(this).find('option:selected'),
        handler = selected.data('onselect');
    if ( typeof handler == 'function' ) {
        handler.call(selected, e);
    }
});
like image 176
David Hellsing Avatar answered Oct 19 '22 13:10

David Hellsing


You can use onchange handler to the select:

<select name='numbers' id='numbers'>
  <option value='1' selected='selected'>One</option>
  <option value='2'>two</option>
  <option value='3'>three</option>
  <option value='4'>four</option>
</select>

<script>
  $('#numbers').change(function () { 
    if ($(this).val() === '1'){
      function1();
    }
    if ($(this).val() === '2'){
      function2();
    }
});
</script>

Best Regards

like image 6
Tabares Avatar answered Oct 19 '22 13:10

Tabares


What you can try is binding the change() event on the select element itself. From there, assuming you have a valid function for each option, you can call an individual option's callback:

$('select').change(function() {
  var type = $(this).val();
  switch(type) {
  }

  // Alternatively, you can try this as well:
  $(this).find('option:selected').each(function() {
  });
});
like image 2
seeming.amusing Avatar answered Oct 19 '22 11:10

seeming.amusing