Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting value from a TextBox after paste

I have a scenario like this where i have dynamically generated textboxes. I have to validate the textbox for max 15 characters and restricting special characters.

Below is the code by which in document.ready() i am generating the textboxes and binding paste events to them.

$(document).ready(function(){

  //Generate textboxes..i have some logic by which i am generating
  //textboxes on the fly and giving textboxes a class flagText
  GenerateFlagBoxes();

  //i am binding the textboxes by class names
   var $flagArea = $('.flagText');
    $flagArea.bind('paste', function () {
        var element = this;
        setTimeout(function () {
            alert($(element).val());
        }, 100);
    });

});

But this is not working.The alert i provided is not coming.I think that the controls are created in the ready event can't be bound to listen events.Am i wrong.I don't know why it is happening. I want some suggestions.

Thanks in advance.

This fiddle is working.I am checking , i might be wrong some where.I will update where i am wrong;

http://jsfiddle.net/mnsscorp/8QFGE/1/

Yes now working .In the document ready i am able to bind paste event.I was wrong some where in the code. :) Thanks for suggestions.

like image 645
mns Avatar asked Apr 24 '13 17:04

mns


2 Answers

Try event delegation for dynamically generated elements -

$(document).on('paste','.flagText',function(){
    var element = this;
    setTimeout(function () {
        alert($(element).val());
    }, 100);
});
like image 157
Mohammad Adil Avatar answered Sep 20 '22 02:09

Mohammad Adil


For dynamic elements, you need an event delegate, such as on.

Try this:

$("body").on('paste', '.flagText', function () {
    setTimeout(function () {
        alert($(this).val());
    }, 100);
});
like image 42
mattytommo Avatar answered Sep 23 '22 02:09

mattytommo