Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass current text as parameter to jquery function

I have this function :

function getlst() {
        $.ajax({
            type: "GET",
            url: "/api/Configuration/GetListParameter",
            success: function (data) {
                EmptyGridStep1();
                for (var i = 0; i < data.length; i++) {   
                    $('#tableBody').append('<tr><td><label id="id" name="id" >' + data[i].id + ' </td>'
                     + '<td>' + data[i].key + '</td>' 
                     + '<td><input type="text"   onchange="keydown();"  value="' + data[i].value + '" /></td></tr>');
                }
                initGrid();
            }
        });
    }

The Keydown function

function keydown(value ) 
{
    alert(value);
}

I'd like to change :

<td><input type="text" onchange="keydown();" value="' + data[i].value + '" /></td>

To pass the current text as a paramater to keydown function.

How can I modify my code to accomplish this task?

like image 401
Lamloumi Afif Avatar asked Oct 22 '25 20:10

Lamloumi Afif


1 Answers

You don't have to do so. All you have to do is pass the input itself as a parameter to your keydown function

<td><input type="text"   onchange="keydown(this);"  value="' + data[i].value + '" /></td>

and just read its value here

 function keydown(input) 
    {
        alert(input.value);
    }

Not the most elegant way to do it, I suggest instead of using onchange, you use jquery's change event , so you have one single listener for your change event

 $('#tableBody').on("change", "input", function() {
  console.log( this.value );
});

You should read this.

like image 54
Quannt Avatar answered Oct 25 '25 11:10

Quannt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!