0

I have a dropdown menu and I want to call a function, when the dropdown menu ITSELF is clicked. I mean the part where you click on the dropdown menu <select> and all the available <options> are displayed.

<select id="p1" title="Select a thing" class="selectpicker show-tick" data-live-search="true">
    <option data-hidden="true"></option>
    <option>Select A</option>
    <option>Select B</option>
</select>

If I want to do something when Select A has been selected, I have the following.

$('#p1').change(function(event) {
    console.log("You selected an option");
});

But how to trigger something if the dropdown menu itself is clicked? I would like to load the <option> with data when the dropdown menu itself is clicked.

The following is not working:

$("#p1").click(function(){
    console.log("Loading <options>");
});

EDIT: I found the problem. It is because I use a plugin called bootstrap-select https://silviomoreto.github.io/bootstrap-select/

If I remove class="selectpicker show-tick" data-live-search="true" it works!

But how do I get on click working with the bootstrap-select plugin?

4
  • 1
    Works perfectly fine for me. jsfiddle.net/v257kjur Commented Jan 26, 2017 at 14:16
  • 1
    What about to do it on focus? Commented Jan 26, 2017 at 14:19
  • 1
    This should be working as it is currently coded. As per Chris G above, it works fine in the fiddle he linked. Other than that, you could try on focus instead of on click Commented Jan 26, 2017 at 14:20
  • I found the "error". Please check my update to the question. It was because of the jquery plugin bootstrap-select. Commented Jan 26, 2017 at 14:35

2 Answers 2

2

Based on your updated code, you can get the click event via the following:

HTML:

<select id="p1" title="Select a procedure" class="selectpicker show-tick" data-live-search="true">
    <option data-hidden="true"></option>
    <option>Select A</option>
    <option>Select B</option>
</select>

jQuery:

$('.bootstrap-select > button[data-id="p1"]').on('click', function (e) {
  console.log('clicked select');
});

Because BootstrapSelect replaces the select with divs and buttons, you'll have to target the correct button as I show above. Note that the ID of your select has now moved into the 'data-id' attribute of the button.

See this new fiddle. Sorry that the styling is messed but the functionality works at least.

Sign up to request clarification or add additional context in comments.

3 Comments

I found the "error". Please check my update to the question. It was because of the jquery plugin bootstrap-select. How coul I get it working with the plugin?
Check my updated answer above for use with the plugin.
Don't forget to choose an accepted answer or update if the answers provided did not solve the issue.
1

The normal on click did not work with the bootstrap-plugin. But they have some documentation regarding events: https://silviomoreto.github.io/bootstrap-select/options/#events

//on click
$('#p1').on('show.bs.select', function (e) {
          console.log("hidden.bs.select means on click");
        });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.