0

I hope this isn't a duplicate because I have not seen anything that I could reference anything from but, I want to focus on the select which is just:

$("#idHere").focus();

I have read that you cannot select the options doing so but I was wondering if maybe you could trigger() the spacebar key once the select has been focused on?

My attempt at this was:

$('body').on('click', '#sid', function () {
  $("#size").attr("size", $("#size").attr("expandto")).css('z-index', 2);
  $("#size").focus().select();
  e = jQuery.Event("keydown");
  fake = $.extend({}, e, { which: 32, ctrlkey: true });
  $("#size").trigger(fake);
});

Is this possible? But this does not work. Reference to idea: here.

Edit: The reason I want to try trigger the spacebar key is because it actually acts like the enter key (or a click).

Edit to make more sense: I want to focus() on the <select> and then trigger the spacebar so the <select> is opened to display the options - all this happens within a click function. Is this possible?

Community
  • 1
  • 1
Jaquarh
  • 6,493
  • 7
  • 34
  • 86
  • 1
    If you set size attribute to select element, it should already be opened. In fact, i don't understand what is your issue? You should provide a sample replicating your issue, like a jsFiddle e.g – A. Wolff Mar 01 '16 at 15:49
  • 1
    I dont think it's possible to programatically a `select` menu. – tymeJV Mar 01 '16 at 15:49
  • I do not have an issue and I don't want it to be constantly open, I want a button to `focus()` on the select and then (if its possible) send a key combination (spacebar). I am asking if its possible to send key combinations because if you can, a spacebar combination should open the `select` menu if its been focused on. **(in theory)** @A.Wolff – Jaquarh Mar 01 '16 at 15:50
  • can't you just trigger() a click on the – ruedamanuel Mar 01 '16 at 15:53
  • I did try that but it didn't actually work, like @tymeJV said. Which is why I am trying to figure out a way around the limitation but of course, this is all theory. Is it actually possible to send key combinations? If so, how? – Jaquarh Mar 01 '16 at 15:54
  • No that is an exact duplicate, this is exactly what I wanted. Thank-you @A.Wolff Apologies! I did try to search for it, I'll vote close! Thanks! – Jaquarh Mar 01 '16 at 16:04
  • 1
    Ok, i was not sure this was exactly what you want :) – A. Wolff Mar 01 '16 at 16:05
  • the wanted result was actual result of the dupe link so we can just say it is haha! Thanks though! @A.Wolff – Jaquarh Mar 01 '16 at 16:07
  • 1
    @KyleE4K You should still test it for cross browser support. Not sure the accpeted answer will work in all cases (it didn't at time it was provided). You could still go with the hacky way like provided in my answer in dupe or better, use a custom select element instead – A. Wolff Mar 01 '16 at 16:11
  • Yes, I altered the code but this is only a local project anyway. I appreciate the concern however! For future reference and better practice, I suppose I should cross-browser reference and debug; thanks! @A.Wolff – Jaquarh Mar 01 '16 at 16:13

0 Answers0