0

I am using jQuery UI's selectmenu to style a <select> element on a page.

By default, it renders a <span> with class .ui-selectmenu-button which toggles the menu.

However, I would like to hide that and make the menu appear/disappear when clicking on a different element on the page.

How would I do this, please?

Note: the currently selected value of the menu does not need to be visible until the menu appears, so that is not a concern.

2
  • Have you tried anything yet? Presumably triggering the menu's event handlers on click of your element would be the way to go, right, or at least worth a try? Those event handlers are all documented on the page you linked to ... what have you tried? Commented Sep 1, 2021 at 8:39
  • Why not open method? See More: api.jqueryui.com/selectmenu/#method-open Commented Sep 1, 2021 at 19:22

1 Answer 1

1

Consider the following example.

$(function() {
  $("#speed").selectmenu();
  $(".next").click(function() {
    $(".select-option").show();
    $("#speed").selectmenu("open");
  });
});
label {
  display: block;
}

select {
  width: 200px;
}

.hidden {
  display: none;
}
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.12.4.js"></script>
<script src="//code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

<div class="car-type ui-widget">
  <label for="car-name" class="ui-widget-header">Name your car:</label>
  <input type="text" name="car-name" id="car-name" class="ui-widget-content"><button class="next btn i-priority-primary">Next</button>
  <div class="select-option hidden">
    <label for="speed" class="ui-widget-header">Select a speed:</label>
    <select name="speed" id="speed">
      <option value="Slower">Slower</option>
      <option value="Slow">Slow</option>
      <option value="Medium" selected>Medium</option>
      <option value="Fast">Fast</option>
      <option value="Faster">Faster</option>
    </select>
  </div>
</div>

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

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.