xrcwrn
xrcwrn

Reputation: 5327

Not able to fetch value of a text box

Not able to fetch value of a text box

I am trying following code

 $(document).on('focusout', '.barcode', function(event){
                event.preventDefault();

                var pid=$(this).parents('.row').attr('id');
                alert(pid);
                var bcd=$(pid+' .barcode').val();
                alert($(pid+' .barcode'));
                alert(bcd);
                var datastr='barCode='+bcd;
                alert(datastr);
           });

I am trying it for following html

<tr class="row" id="item10">
                                    <td><input type="text" name="barcode" class="barcode" style="width: 50px"/></td>
                                    <td><input type="text" name="type" class="type" style="width: 50px"/></td>
                                    <td><input type="text" name="color" class="color" style="width: 50px"/></td>
                                    <td><input type="text" name="qty" class="qty" style="width: 50px"/></td>
                                    <td><input type="text" name="price" class="price" style="width: 50px"/></td>

                                </tr>


   value of var `id` is correct but `bcd` and `dataStr` is not correct.

Please see what is the problem

Upvotes: 0

Views: 77

Answers (3)

Bhadra
Bhadra

Reputation: 2104

       $(document).on('focusout', '.barcode', function(event){
            event.preventDefault();
            alert($(this).val());
       });

you can just use $(this).val() to get value of that element

Upvotes: 2

Tsiftelis Thanasis
Tsiftelis Thanasis

Reputation: 41

Also, is you code inside a

$(document).ready(function () {
......
});

Upvotes: 0

Anton
Anton

Reputation: 32591

You are missing "#" id indicator

var bcd=$("#"+pid+' .barcode').val();

or

var pid="#"+$(this).parents('.row').attr('id');

And <tr> must be wrapped with <table> incase yours isn't.

DEMO

Upvotes: 3

Related Questions