Search input autofocus on Iphone
-
When a Iphone users clicks on Search icon, we want the search field to be marked so that you can directly start typing in what you want to search for. Now you need to click on the search field again. For Android users, when clicked, the keyboard appears immediately. Is it possible to solve?
The page I need help with: [log in to see the link]
-
Can anyone help with writing the code correctly?
add_action( 'wp_head', 'fibosearch_wp_head' ); function fibosearch_wp_head() { ?> <script> window.addEventListener('load', function() { if ( typeof jQuery !== 'undefined' ) { var button = jQuery('.dgwt-wcas-search-wrapp, .dgwt-wcas-search-icon, .dgwt-wcas-ico-magnifier, .dgwt-wcas-ico-magnifier-handler'); button.on( 'click', function() { window.setTimeout(function(){ jQuery('.dgwt-wcas-search-input, .dgwt-wcas-search-input-2, .dgwt-wcas-search-form').focus(); }, 200); } ); } }, false); </script> <?php }-
This reply was modified 2 years, 5 months ago by
Volodymyr.
Please try this code snippet:
// FiboSearch mobile autofocus search input fix on iOS add_action( 'wp_footer', function() { ?> <script type="text/javascript"> jQuery(document).ready(function($) { var searchIcon = $('.js-dgwt-wcas-layout-icon-flexible'); var searchInput = $('.elementor-element-bf5b3a6 .dgwt-wcas-search-input'); setTimeout(function() { if (searchIcon.length && searchInput.length) { $('body').on('click', searchIcon, function() { setTimeout(function() { searchInput.focus(); }, 100); }); // iOS Safari $('body').on('touchstart', searchIcon, function() { setTimeout(function() { searchInput.focus(); }, 100); }); } }, 150); }); </script> <?php }, 9999 );You have two ways to add this code to your theme:
- Open the
functions.phpin your child theme and add the code at the end. - or install the Code Snippets plugin and apply this code as a snippet.
Regards,
KrisThanks for the answer, I installed it, but the code doesn’t work ;(
People don’t use this solution – it doesn’t work
Hi, @bullshmaiser!
Let’s try this improved code where we:
- Removing unnecessary delay: We reduce the delay of calling the function to 0 ms.
- Changing the event registration method. Instead of using
on('click', ...)andon('touchstart', ...), we will use theclick()andtouchstart()methods directly on the elements.
add_action( 'wp_footer', function() { ?> <script type="text/javascript"> jQuery(document).ready(function($) { var searchIcon = $('.js-dgwt-wcas-layout-icon-flexible'); var searchInput = $('.elementor-widget-container .dgwt-wcas-search-input'); if (searchIcon.length && searchInput.length) { searchIcon.click(function() { setTimeout(function() { searchInput.focus(); }, 0); }); // For iOS Safari, trigger autofocus when search icon is touched searchIcon[0].addEventListener('touchstart', function() { setTimeout(function() { searchInput.focus(); }, 0); }); } }); </script> <?php }, 9999 );Regards,
Krisdoes not work. I left the code on the site so you can test it too
Hi @bullshmaiser!
In the upcoming version of the FiboSearch, we will try to fix this issue. However, it’s important to note that this attempt comes with no explicit guarantee of success.
Regards,
KrisTell me, have you fixed it?
Hi @bullshmaiser!
No, we still working on it, but can you test my two new solutions?
1st:
add_action( 'wp_footer', function() { ?>
<script type="text/javascript">
jQuery(document).ready(function($) {
var searchIcon = $('.js-dgwt-wcas-search-icon-handler');
var searchInput = $('#dgwt-wcas-search-input-2');
if (searchIcon.length && searchInput.length) {
searchInput.attr('tabindex', '-1');
searchIcon.click(function() {
requestAnimationFrame(function() {
searchInput.focus();
});
});
searchIcon[0].addEventListener('touchstart', function() {
requestAnimationFrame(function() {
searchInput.focus();
});
});
}
});
</script>
<?php }, 9999 );2nd:
add_action( 'wp_footer', function() { ?>
<script type="text/javascript">
jQuery(document).ready(function($) {
var searchIcon = $('.js-dgwt-wcas-search-icon-handler');
var searchInput = $('#dgwt-wcas-search-input-2');
if (searchIcon.length && searchInput.length) {
searchInput.attr('tabindex', '-1');
function focusInput() {
searchInput.focus();
}
searchIcon.on('click touchstart', function() {
$('.elementor-element-bf5b3a6').addClass('visible');
focusInput();
});
}
});
</script>
<?php }, 9999 );Please let me know if any of these solutions will work.
Regards,
Kris-
This reply was modified 2 years ago by
Kris.
thanks for the help, but this doesn’t work either
-
This reply was modified 2 years, 5 months ago by
The topic ‘Search input autofocus on Iphone’ is closed to new replies.