jQuery: To check if an element ID is used

A simple way to check if an element ID is used.

<!DOCTYPE html>
<html>
    <head>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
        <title>HTML element ID check</title>      
    </head>
    <body>
        <div id = "myDiv">HTML Div Element</div>
        <script>
            //jquery if exists DOM element
            $(function () {
                if ($('#myDiv').length) {
                    $('#myDiv').html('ID is found');
                }
            })
        </script>
    </body>
</html>

button

Leave a comment

Leave a Reply