Created a new input element and tried adding the data attribute using the data method in jquery but am not able to append the value
var txtBox = $('<input/>', {
"class": "myCustomClass",
"type": "text"
});
$('#wrapper').append(txtBox);
txtBox.data('index', '1');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id='wrapper'>Input Text Box : </div>
data()does not update the DOM, so thedata-indexattribute will not appear in the inspector. So long as you use the getter ofdata()to retrieve the value, you will have no issues:var index = txtBox.data('index')attrmethod.