After the ajax function is over. in success messages I'm focusing to the specific div. But it's not working. My code is here.
$j.ajax({ url:"<?php echo admin_url( 'admin-ajax.php' ); ?>", type:"POST", data:"action=press_release&page="+0+"&do_task="+do_task+"&id="+id+"&module="+module, success:function(data){ $j("#com_cont").show(); $j("#com_cont").html(data); $j("#loading_heart").hide(); $j("#focus_point").focus(); } });
This is the code is not working(Not focusing on the div:$j("#focus_point").focus();
The reason that's not working is simply because it's not stealing focus from the dev console. If you run the following code in your console and then quickly click in your browser window after, you will see it focus the search box: setTimeout(function() { $('input[name="q"]'). focus() }, 3000);
The focus() is an inbuilt method in jQuery which is used to focus on an element. The element get focused by the mouse click or by the tab-navigating button. Here selector is the selected element. Parameter: It accepts an optional parameter “function” which specifies the function to run when the focus event occurs.
focus() on elements that are visible. To run an element's focus event handlers without setting focus to the element, use . triggerHandler( "focus" ) instead of . focus() .
jQuery focus() MethodThe focus event occurs when an element gets focus (when selected by a mouse click or by "tab-navigating" to it). The focus() method triggers the focus event, or attaches a function to run when a focus event occurs. Tip: This method is often used together with the blur() method.
a <div>
can be focused if it has a tabindex
attribute. (the value can be set to -1)
For example:
$("#focus_point").attr("tabindex",-1).focus();
In addition, consider setting outline: none !important;
so it displayed without a focus rectangle.
var element = $("#focus_point"); element.css('outline', 'none !important') .attr("tabindex", -1) .focus();
you can use the below code to bring focus to a div, in this example the page scrolls to the <div id="navigation">
$('html, body').animate({ scrollTop: $('#navigation').offset().top }, 'slow');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With