13

Let’s say I have this markup:

<div id="content">
  <div id="firstP"><p>First paragraph</p></div>
  <div id="secondP"><p>Second paragraph</p></div>
  <div id="thirdP"><p>Third paragraph</p></div>
  <div id="fourthP"><p>Fourth paragraph</p></div>
</div>

I want to add a new <div> with jQuery and focus on this new element. .focus does not do anything.

function addParagraph() {
  var html = "<div id=\"newP\"><p>New paragraph</p></div>";

  $("#content").append(html);
  $("#newP").focus();    
}

Any idea why?

Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
Sergio del Amo
  • 76,835
  • 68
  • 152
  • 179

8 Answers8

50

I think the main answer is incorrect. DIV and P tags can receive focus providing you specify a tabindex property for them. i.e.

<div class="someclass" tabindex="100">

Once the tabindex is specified you can either tab to these elements or shift focus with .focus() .

Using a scrollTo plugin seems like a bit of an overkill here.

Michal
  • 501
  • 4
  • 2
  • This helped me figure out how to process some keyboard commands in a tree container div. By assigning a keypress event handler to a div with a tabIndex value, I was able to add keyboard commands to a hierarchical data editor. – 3Dave Sep 04 '09 at 15:32
  • Perfect! Yours is the best solution in terms of being simple. I like simple stuff. I prefer to avoid a script if it can be done without it. – Marcos Buarque Oct 21 '09 at 17:41
  • This was a great solution. Thank you very much. – Corv1nus Aug 03 '11 at 20:26
  • 5
    You should use tabindex="0" if you don't want to disturb the order of the tabs in the page. If you only want to give focus with Javascript, and make the div not focusable, put tabindex="-1". Tabindex -1 can receive focus by Javascript, but not by hitting the tab key. – dturcotte Jun 01 '12 at 15:04
17

There's no problem with your code, it's just that a paragraph or div tag can't receive focus. Focus can only be given to things you can interact with, such as links, input elements, textareas, etc.

To scroll the window to this newly added element, you can use a plugin such as ScrollTo.

On a side note, your code could be simplified a bit:

var html = "<div id=\"newP\"><p>New paragraph</p></div>";
$("#content").append(html);
$("#newP p").focus();

var html = "<div id=\"newP\"><p>New paragraph</p></div>";
$(html)
    .appendTo('#content')
    .focus()   // or scrollTo(), now...
;
nickf
  • 537,072
  • 198
  • 649
  • 721
6

This code will avoid dependencies on other plugins and allow you to have it on any element.

$('html, body').animate({ scrollTop: $("#newP").offset().top }, 500);
User123342234
  • 1,455
  • 14
  • 17
5

You need to use a HTML page anchor not focus. Example:

http://localhost/mypage.html#fourthP
Todd Smith
  • 17,084
  • 11
  • 59
  • 78
2

I think what you're looking for is using the 'ScrollTo' plugin in jQuery. You can check it out here.

You can specify what to scroll...

$('div.pane').scrollTo(...);//all divs w/class pane

Or just scroll the window:

$.scrollTo(...);//the plugin will take care of this

There are many different ways to specify the target position. These are:

  • A raw number
  • A string('44', '100px', '+=30px', etc )
  • A DOM element (logically, child of the scrollable element)
  • A selector, that will be relative to the scrollable element
  • A hash { top:x, left:y }, x and y can be any kind of number/string like above.

Bonus: In digging up this information, I also found LocalScroll and SerialScroll (animates scrolling from one item to the next).

Dan Esparza
  • 28,047
  • 29
  • 99
  • 127
1

You can assign tabidex and then you can set focus to that element

$('#table').attr("tabindex",1).focus();

This will work for Div Table etc which can't get focus by itself.

Avinash
  • 173
  • 1
  • 4
  • 17
1

Only form elements and such can attain focus. If you want the browser to scroll down to that particular paragraph, there is no "default" way of doing so with jQuery, but there is a plugin for it at jQuery.ScrollTo and a blog explaining how to do it manually at Animated scroll with jQuery

Guðmundur Bjarni
  • 4,082
  • 1
  • 18
  • 14
1

Instead of

$("#newP").focus();  

it should be used:

window.location.href=window.location.href + "#newP";     
Sergio del Amo
  • 76,835
  • 68
  • 152
  • 179