D-W D-W - 4 months ago 20
jQuery Question

jquery pass id and value on input keyup

Im dynamically creating rows, but I then also need to pass in the id of the control and the value

function AddClient() {
var uuid = guid();
$("#clientList").append("<row><input type='text' placeholder='type to search' name='clientName' id='ClientName" + uuid + "' class='form-control' onkeyup='SearchClient($(this).attr('id'),$(this).val())'></row>")


function SearchClient(id, term) {
//do some lookups
}


get error
(index):2 Uncaught SyntaxError: Unexpected token }


My problem is
$(this).attr('id')
, any ideas what I should replace this with?

Answer

You can do like this

function AddClient() {
    var uuid = guid();
    $("#clientList").append("<row><input type='text' placeholder='type to search' name='clientName' id='ClientName" + uuid + "' class='form-control' onkeyup='SearchClient(this)'></row>");
}


function SearchClient(elemt) {
    id= $(elemt).attr('id');
    term= $(elemt).value();
    //do some lookups
}