jquery : focus to div is not working

Jquery

Jquery Problem Overview


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();

Jquery Solutions


Solution 1 - Jquery

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();

Solution 2 - Jquery

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');

Solution 3 - Jquery

Focus doesn't work on divs by default. But, according to this, you can make it work:

> The focus event is sent to an element > when it gains focus. This event is > implicitly applicable to a limited set > of elements, such as form elements > (<input>, <select>, etc.) and links > (<a href>). In recent browser > versions, the event can be extended to > include all element types by > explicitly setting the element's > tabindex property. An element can gain > focus via keyboard commands, such as > the Tab key, or by mouse clicks on the > element.

http://api.jquery.com/focus/

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionArungView Question on Stackoverflow
Solution 1 - JquerysuDockerView Answer on Stackoverflow
Solution 2 - JqueryderekView Answer on Stackoverflow
Solution 3 - JqueryAmy AnuszewskiView Answer on Stackoverflow