[jquery] jquery : focus to div is not working

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

This question is related to jquery

The answer is


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

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

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/