Namit
Namit

Reputation: 1322

Getting value of dynamically generated textfield

I have a table that has been generated dynamically using PHP. The table has a few textfields which are being used to update the database based on the row. This question can be referred at: Complete question, based on one for the answer i am using javascript which gets the textfield value when enter is pressed. The javascript being used is:

$("#details input").live('keypress', function (e) {
    var code = (e.keyCode ? e.keyCode : e.which);
    if (code == 13) {
        e.preventDefault();
        var ourId = $(this).id;
        var updatedText = $(this).val;

        alert(updatedText);
    }
});

However for some reason $(this) isn't returning the expected result.

Any help will be appreciated, thanks.

Upvotes: 0

Views: 251

Answers (2)

elclanrs
elclanrs

Reputation: 94101

You're mixing concepts there. $(this).id --> this.id OR $(this).attr('id') and $(this).val --> $(this).val(). Instead of live() which is deprecated, use on() for delegation.

$("#details").on('keypress', 'input', function(){
    // Bla
});

Upvotes: 0

ShankarSangoli
ShankarSangoli

Reputation: 69915

val is a method and not property so it be should used like a function.

Change $(this).val to $(this).val()

And to get the id of the input element just use this.id. id is a property of the dom elemdnt.

Upvotes: 4

Related Questions