<div id="test"></div> <script> $(document).ready(function() { alert($('#test').id); }); </script>
Why doesn't the above work, and how should I do this?
The jQuery way:
$('#test').attr('id')
In your example:
$(document).ready(function() { console.log($('#test').attr('id')); });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="test"></div>
2.1m questions
2.1m answers
60 comments
57.0k users