pal3
pal3

Reputation: 241

get textbox value from ajax datatable

can someone help regarding my problem getting the value of a textbox from ajax datatable? below is my code.

My controller:

public function getmovielist (Request $request, Response $response)
{
    $cart = Cart::where('category', 'action')->get();

    $output = [];

    foreach($cart as $c) {
        $output[] = [
            '<input type="number" class="form-control" min="0" id="movie_id" name="qty">',
            $c->title,
        ];
    }

    return $response->withJson(["data"=>$output]);
}

My JS script(load json response into my table)

var ct = $('#movie_table').DataTable({
    "ajax": {
        url : 'getmovielist',
        type : 'GET'
    },
});

My View

+----------+----------+
| textbox  |John wick |
+----------+----------+
| textbox  |Deadpool  |
+----------+----------+

Here is my code to get the value of the textbox on blur

$('#movie_id').on('blur', function(){
    console.log(this.val());
});

but I got nothing in my console log. Thanks

Upvotes: 1

Views: 321

Answers (2)

Vipul
Vipul

Reputation: 1583

I would suggest to use class selector rather id selector as id selector works for single element.

You can change your code like below and then try

    foreach($cart as $c) {
            $output[] = [
                '<input type="number" class="form-control txtmovie" min="0" id="movie_id" name="qty">',
                $c->title,
            ];
        }
-----------------------------
$( document ).ready(function() {
    $('.txtmovie').on('blur', function(){
        console.log(this.val());
    });
});   

Upvotes: 1

user10064408
user10064408

Reputation:

Your function is wrong. I used $('body') because if you generate element dynamically with ajax, delegate events should be at the closest static element.

$('body').on('blur', '#movie_id', function(){
 console.log(this.val());
});

Upvotes: 0

Related Questions