$(document).ready(function() {
$('input').each(function() {
if ($(this).name != '') {
var label = $("<label >").attr('style', "display:none !important;").attr('for', $("input").attr("id")).text($("input").attr("name"));
$('input').append(label);
}
});
});
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<head>
<body>
<table id="myTable">
<tbody>
<tr>
<td>Account Reference</td>
<td><input type="text" name="account_reference_textbox" id="account_reference_textbox" maxlength="35" value="">
</td>
<td>Forename</td>
<td><input type="text" name="first_name_textbox" id="first_name_textbox" maxlength="10" value=""></td>
</tr>
<tr>
<td>Customer Reference</td>
<td><input type="text" name="customer_reference_textbox" id="customer_reference_textbox" maxlength="25" value="">
</td>
<td>Surname</td>
<td><input type="text" name="last_name_textbox" id="last_name_textbox" maxlength="20" value=""></td>
</tr>
<tr>
<td>Company Name</td>
<td><input type="text" name="company_name_textbox" id="company_name_textbox" maxlength="20" value=""></td>
</tr>
</tbody>
</table>
</body>
<html>
I am trying to create label for each input of type text using its attribute name & Id using Jquery.
I have successfully created a label by using input attributes but failed to get its correct position.
input, you should use$(this).attr('name').$(this).namewill always returnundefinedin your case.