www.webdeveloper.com
Results 1 to 4 of 4

Thread: jQuery focus() issue.

  1. #1
    Join Date
    May 2012
    Posts
    29

    jQuery focus() issue.

    Dear JavaScript Expert.

    I would like the cursor / caret to be focused in my newly created text area when the below event is triggered. For some reasons it's not happening. Perhaps I am not understanding the jQuery documentation. Can you see the below code and advise?

    Thank you.

    Code:
    $(".addTextArea").click(function(){
    	
    	var $this = $(this);
    	
    	//clone a new textArea
    	var $textArea= $("#textAreaTemplate").clone(true);
    	
    	//assign a unique identifier.  Note intID is a global incrementer.  In the actual code it gets incremented.
    	$textArea.attr("id", "textArea" + intId);
    	
    	//*** doesn't put the caret into the new text Area.
    	$this.parent().after($textArea).focus();
    	
    	//*** this doesn't work either
    	$("#textArea" + intId).focus();
    	
    });
    Last edited by cations; 06-06-2012 at 09:26 PM.

  2. #2
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,637
    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <title>...</title>
    <style type="text/css">
    body{font-family:Georgia,Verdana,Arial;font-size:13px;background-color:#fff;color:#000;padding:30px 0px 0px 300px;margin:0px 0px 0px 0px;}
    .newtextarea{display:block;margin-top:15px;}
    .addTextArea{margin-left:30px;}
    </style>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function(){
    $(".addTextArea").click(function(){
    var tarlen=$('textarea').length+1;
    $('#textAreaTemplate').clone().appendTo($(this).parent()).addClass('newtextarea').attr('id','textArea_'+tarlen).attr('title','textArea_'+tarlen).focus();
    });
    });
    </script>
    </head>
    <body>
    <div>
    <textarea id="textAreaTemplate" rows="4" cols="20"></textarea><input class="addTextArea" type="button" value="Add a new textarea" />
    </div>
    </body>
    </html>
    xxx: Guess Buddhist riddle: "What is the sound of one hand clapping?"
    yyy: facepalm

  3. #3
    Join Date
    Jul 2003
    Location
    The City of Roses
    Posts
    2,503
    Quote Originally Posted by cations View Post
    Code:
    $this.parent().after($textArea).focus();
    The problem is that the node set jQuery is operating on is still $this.parent(), not $textArea. You'll probably have to write the focus as a separate statement.

    Code:
    $this.parent().after($textArea);
    $textArea.focus();
    for(split(//,'))*))91:+9.*4:1A1+9,1))2*:..)))2*:31.-1)4131)1))2*:3)"'))
    {for(ord){$i+=$_&7;grep(vec($s,$i++,1)=1,1..($_>>3)-4);}}print"$s\n";

  4. #4
    Join Date
    May 2012
    Posts
    29
    I got it to work. Thank you.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles