3

I have a script where I append a table row to my html (inside the row there is a text field):

     $('#resulttable').append('<tr><td>'+this.id+'</td><td>'+ this.customer_name + '</td><td>'+ this.total +'</td>'+'<td><input type = "text" id ='+this.id+' name = "paid" onChange="change_send(this.value, this.id);"></td></tr>');

Here I am sending two parameters to my change_send function. I want some more parameters, say for example this.bill_id. How can I add that in my text field and send it as the third parameter?

2 Answers 2

2

I would refrain of using this kind of way to add handler. What I suggest is that you use event delegation and data-* attributes to store the needed information.

//Event has to be declared only once, it will work on all rows.
$('#resulttable').on('change','input',function(e){
    change_send(this.value, this.id, $(e.target).data('bill_id'));
});

$('#resulttable').append('<tr><td>'+this.id+'</td><td>'+ this.customer_name + '</td><td>'+ this.total +'</td>'+'<td><input type="text" id='+this.id+' name="paid" data-bill_id="'+this.bill_id+'"></td></tr>');

That way, you could add more data to your input, and have your function receive it. There are other ways, but this seems like the easiest for now.

Sign up to request clarification or add additional context in comments.

Comments

1

You can use for example data attributes, so your input should look like this:

<input type="text" id='1' name="paid" data-billid="12" onChange="change_send(this.value, this.id, this.data, this.dataset['billid'])">

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.