Pages

Sunday, December 13, 2015

JQuery Selector - Select control by #Id

Selector plays a vital role in JQuery. They are used to select the control.
You can select a control by its Id, Class, tag etc.

Id Selector:

Id selector is used to select a control by its Id. We are using ’#’ in syntax.

Syntax:  $(‘#control-id’).event (…..)

<head>
    <title></title>
    <script src="Scripts/jquery-1.10.2.js"></script>
    <script type="text/javascript">
        $(document).ready(
            function () {
 $("#Button1").css("color","Red")
            });
    </script>
</head>
<body>
    <input id="Button1" type="button" value="click" />
</body>

================================================================

Id Selector returns the collection of objects, if html page has multiple controls with the same Id.

If you want to find the control is exists or not on a page you can use the length property to get the length of the collection return by id selector.

<head>
    <title></title>
    <script src="Scripts/jquery-1.10.2.js"></script>
    <script type="text/javascript">
        $(document).ready(
            function () {
                if ($('#Button2').length > 0) {
                    alert("Button2 Exists.")
                }
                else
                    alert("Button2 Not Exists.")
            });
    </script>
</head>
<body>
    <input id="Button2" type="button" value="click" />

</body>


=========================================================================================








No comments:

Post a Comment