3

my code is this. on jsfiddle.

var inp = $('<input>').attr({'class':'txtEdit txt', 
    'type':'text'})
    .val($(this).html()).blur(function (){
        if($(this).val().length != ''){
            $(txt).replaceWith($(editable)
                .html($(this).val()));
        }
        else{ alert("Cannot contain empty value!")}
    }).keydown(function (e){
            if(e.keyCode == 13 || e.keyCode == 9){
                e.preventDefault();
                $(this).trigger('blur');
            }
    }).appendTo($(txt));

I am creating an input element with lots of events and adding to div. Now i want to focus it as it is appended to DOM. but focus does not seem to be working.

Here's the total code.

<html>
    <head>
        <title>My jQuery plugin</title>
        <script src="jquery.js"></script>
        <script>
            $(function ()
            {
                    $('.editable').txtEdit();
            });

            (function($){
                $.fn.txtEdit = function() {
                    var editable = $(this);

                    $(editable).live('click', 
                        function ()
                        {
                            var txt = $('<div>').attr('class', 'txtEdit div');
                            var inp = $('<input>').attr({'class':'txtEdit txt', 
                                            'type':'text'})
                                        .val($(this).html()).blur(function (){
                                                if($(this).val().length != ''){
                                                    $(txt).replaceWith($(editable)
                                                        .html($(this).val()));
                                                }
                                                else{ alert("Cannot contain empty value!")}
                                            }).keydown(function (e){
                                                    if(e.keyCode == 13 || e.keyCode == 9){
                                                        e.preventDefault();
                                                        $(this).trigger('blur');
                                                    }
                                            }).appendTo($(txt));
                            $(this).replaceWith(txt);                           
                        }
                    );
                };
            })(jQuery);
        </script>
    </head>
    <body>
        <div class="editable">this is editable div</div>
    </body>
</html>

Anyone any ideas??

Regards,

S L
  • 14,262
  • 17
  • 77
  • 116

4 Answers4

4

add

inp.focus();

after

$(this).replaceWith(txt);

Because your div is created after focus action, which is wrong. You should display the div and then focus on input.

benck
  • 2,034
  • 1
  • 22
  • 31
1

Instead of chaining the focus add this after creating the input box.

inp.focus();
Jake
  • 2,471
  • 15
  • 24
1

Just do

inp.focus();

after the $(this).replaceWith(txt);

What you were doing is trying to focus the element before you add it to the DOM (while it was still in memory).

live at http://jsfiddle.net/gaby/KH7pZ/8/

Gabriele Petrioli
  • 191,379
  • 34
  • 261
  • 317
1

You need to put

inp.focus();

At the end of your code (once the replacement is done);

Here's your updated fiddle : http://jsfiddle.net/KH7pZ/9/

JohnP
  • 49,507
  • 13
  • 108
  • 140