xblade52 xblade52 - 1 month ago 6
HTML Question

Convert or extract value from input to plain text

I've got this js function that converts ALL input values into text.
I want to convert only ONE row from to plain text (all rows have 5 cells plus a button for saving) and with that button i want to convert that row data into plain text. I was trying to use .closest object but i failed.

function disable_inp() {
$("tr:gt(0) td:has(input)").text(function() {
return $('input', this).val();
});
}

Answer

You should use :eq and not :gt. The eq gets the index of the selection, while gt is greater than.

function disable_inp() {
  $("tr:eq(0) td:has(input)").text(function() {
    return $('input', this).val();
  });
}

And with the button, you can do the same:

$("button").click(function disable_inp() {
  $(this).closest("tr").find("td:has(input)").text(function() {
    return $('input', this).val();
  });
});

Here, the .closest() will find the first parent that matches the selection and applies from there.

Comments