I am new to jquery, I tried to access the input element via id , I see it returns "undefined" I wrapped it inside document.ready; result is the same, here is the code snippet
let input;
function maintest() {
input = $("<input>").attr({"id":"test"});
input.append("#mainDiv");
$(document).ready( function(){
alert($("#test").html());
});
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<body onload="maintest()">
<div id="mainDiv"></div>
</body>
I am surprised it shows "undefined" instead of showing the whole input element when I alert
inputwhen you use$(input).attr({"id":"test"})?