Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery : focus to div is not working

Tags:

jquery

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

like image 271
Arung Avatar asked May 11 '11 14:05

Arung


People also ask

Why focus is not working jQuery?

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

Can we focus on a div jQuery?

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.

Which jQuery method can be a proper replacement for focus () in order to run the element's focus event handler without setting the focus on the element?

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

What is focus in jQuery?

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.


2 Answers

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(); 
like image 116
suDocker Avatar answered Sep 19 '22 22:09

suDocker


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'); 
like image 30
derek Avatar answered Sep 23 '22 22:09

derek