dido
dido

Reputation: 2361

How can I changing text to input and geting input value with jQuery?

I have following problem. After clicking on text I want to changing this text with input. After insert value in this input and press ENTER I want to returning input value.

I using this code but not worked after click enter :( :

$(document).ready(function() {

    $('.qty_new').click(function(){
        $(this).replaceWith("<input class='newVal' value='' />");
        return false;

        $('.newVal').keypress(function(e){
            var qty=$(this).val();
            var code =null;
            code = (e.keyCode ? e.keyCode : e.which);
            if (code == 13){
               alert('ratata');
            } 
        });
    });


    <span class='qty_new'>100</span>

Thanks in advance !

Upvotes: 0

Views: 53

Answers (1)

Elen
Elen

Reputation: 2343

please see my fiddle - code works fine

http://jsfiddle.net/R6z9S/

just get rid off return false;


$(document).ready(function() {

    $(document).on('click','.qty_new',function(){
        $(this).replaceWith("<input class='newVal' value='' />");

        $('.newVal').keypress(function(e){
            var qty=$(this).val();
            var code =null;
            code = (e.keyCode ? e.keyCode : e.which);
            if(e.keyCode == 13) {
                alert('You pressed enter! '+qty);
            }
        });
    });
})

Upvotes: 1

Related Questions