Alex Rivera | Logout

jQuery .focus doesn’t actually focus a newly created element

Asked 2008-12-02T16:09:04.357
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?

Edit
Report

1 Answer

1

Instead of

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

it should be used:

window.location.href=window.location.href + "#newP";     
answered 2008-12-02T16:18:18.330

Your Answer