Related Posts Plugin for WordPress, Blogger...

About

Follow Us

Saturday, 28 March 2015

Here in this article I will explain how to use jQuery to get or find elements with class name or id. We can get elements (such as div) by using class name or id easily in jQuery.

 

Jquery Code to Find Div Using Class Name:

<script type="text/javascript">
    $(function () {
        $('#btnGet').click(function () {
            var classval = $('.testcalss').html();
            alert(classval)
        });
    })

</script>

Jquery Code to Find Div Using ID:

<script type="text/javascript">
    $(function () {
        $('#btnGet').click(function () {
            var idval = $('#testiddiv').html();
            alert(idval)
        });
    })

</script>

 

Full Source Code:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>jQuery Get elements with id or css class names with example</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>
<script type="text/javascript">
    $(function () {
        $('#btnGet').click(function () {
            var idval = $('#testiddiv').html();
            var classval = $('.testcalss').html();
            alert(idval)
            alert(classval)
        });
    })
</script>
</head>
<body>
<form id="form2">
<center>
 <fieldset  style="width:300px">
            <legend><strong>Find Element using jQuery</strong></legend>
<div id="testiddiv" style="background:yellow">
Find Div Using ID
</div><br />
<div  class="testcalss"> Find Div using Class Name</div>
<br />
<input type="button" id="btnGet" value="Find Divs By Id or Class" />
</fieldset></center>
</form>
</body>

</html>
Categories: ,

0 comments:

Post a Comment