简体   繁体   中英

How to pass a variable as a function parameter

I have this function that turns cells with class editable ( td.editable ) to input fields. Now, you have to pass the row to the function. For example: editRow(myRow) .

So using this.parentNode.id , I get the id of the row. But when I pass the value as a parameter to the editRow function, it does nothing.

I think the function thinks that rowId is the actual name of the row, instead of using the contents of the rowId variable as the name of the row.

function editRow(row) {
 $('td.editable',row).each(function() {
  $(this).html('<input type="text" value="' + $(this).html() + '" size="10" />');
 });
}    

/* click on cell with .editable class */
 $(".editable").click(function() {
   rowId = this.parentNode.id;
   editRow(rowId);
});

editRow doesn't take a row id, it takes an entire row (the DOM Element itself). You're simply passing the wrong data. Try:

$('.editable').click(function(){
    editRow(this.parentNode);
});

Given your function here:

/* click on cell with .editable class */
$(".editable").click(function() {
rowId = this.parentNode.id;
editRow(rowId);
});

You are passing the id to your editRow function. So inside editRow, you can just do this:

function editRow(row) {
    $('#' + row +' td.editable').each(function() {
        $(this).html('<input type="text" value="' + $(this).html() + '" size="10" />');
    });
}

'#' is the jquery id selector

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM