2

I have tried below code:

  1. Set the value in search text box
jQuery("#inplaceSearchDiv_WPQ2_lsinput").val("med");
  1. Trigger Enter key:
var e = jQuery.Event("keypress");
e.which = 13 
e.keyCode = 13
jQuery('#inplaceSearchDiv_WPQ2_lsinput').focus();
jQuery('#inplaceSearchDiv_WPQ2_lsinput').trigger(e);

But enter key is not triggering by above code, please help me on it

5
  • Do you want to search after pressing enter key or you want to trigger enter event programatically? Dec 24, 2020 at 11:23
  • I want to trigger enter event programmatically. Dec 24, 2020 at 11:28
  • Are you getting any error in console? Or no error but event is not triggering? Please check if you are selecting your input textbox correctly using jQuery selector. Dec 24, 2020 at 13:32
  • could you show html of your input?
    – Raf
    Dec 24, 2020 at 14:18
  • Hi @Raf, below is the html of input: <input id="inplaceSearchDiv_WPQ2_lsinput" title="Type something and hit enter to search this library" class="ms-helperText ms-textSmall ms-InlineSearch-SearchBox-Baseline ms-InlineSearch-SearchBox-EmptyUnfocused" type="text" value="" maxlength="2048"> Dec 28, 2020 at 9:26

1 Answer 1

1

Try this:

$('<text-input-selector>').trigger(
    jQuery.Event('keypress', { keyCode: 13 })
);

Replace <text-input-selector> with appropriate jQuery selector for your input field.

If you are not able to get the correct selector then let me know and share the screenshot of textbox element by inspecting it. I will help you with jQuery selector.

Update from Comments:

Check below code. I have tested this and working fine for me:

//Set textbox value
$("input[title^='Search this site']").val(".pdf");

//Trigger Enter event on textbox
var e = jQuery.Event("keypress");
e.which = 13;
e.keyCode = 13;

$("input[title^='Search this site']").trigger(e);
5
  • Sorry Ganesh, this is not working. Dec 24, 2020 at 12:15
  • Check my updated answer. I have tried it on my SharePoint site and working fine for me. Use appropriate jQuery selector for your input textbox Dec 24, 2020 at 13:24
  • Hi Ganesh, Have you tried on Library search? I have used below code, but no success //Set textbox value jQuery("input[title^='Type something and hit enter to search this library']").val(".pdf"); //Trigger Enter event on textbox var e = jQuery.Event("keypress");e.which = 13;e.keyCode = 13; jQuery("input[title^='Type something and hit enter to search this library']").trigger(e); Dec 28, 2020 at 9:11
  • Do you have jQuery file loaded on the page where you are executing this code (You will require jQuery to execute above code)? Are you getting any error in browser's console? Are you able to access the input element using the jQuery selector you mentioned in your code? Dec 28, 2020 at 13:09
  • I am executing this code in console of browser and not getting any error in console. Dec 28, 2020 at 13:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.