Get element with CSS class name or id with jQuery

Get element with CSS class name or id with jQuery, jQuery is extremley powerful and a time (and even life) saver, using jQuery, you can get elements with CSS class name and id easily without too much blood and tears!

How to Get element with CSS class name or id with jQuery

For example, ID: #idA

$(‘#idA’) – would select all elements that have an id of ‘idA’, regardless of its tag name.
$(‘div#idA’) – would select all div elements that have an id of ‘idA’.

And when using Class: .classname

$(‘.classA’) – would selects all elements that have an class name of ‘classA’.
$(‘div.classA’) – would select all div elements that have a class name of ‘classA’.

Here is a complete example;

<html>
<head>
<title>Using jQuery to get an element with class name and/or id</title>

<script type="text/javascript" src="jquery-1.3.2.min.js"></script>

</head>

<script type="text/javascript">

$(document).ready(function(){

var $element = $('.classABC').html();
alert($element);

var $element = $('#idABC').html();
alert($element);

});

</script>
<body>

<h1>jQuery Get element with class name and id</h1>

<div class="classABC"> This is belong to 'div class="classABC"'.</div>

<div id="idABC">This is belong to 'div id="idABC"'</div>

</body>
</html>

This Post Has 3 Comments

  1. Lucinda Clayborne

    Nice post. I was checking constantly this blog and I am impressed! Extremely helpful information specifically the last part 🙂 I care for such information a lot. I was looking for this certain information for a very long time. Thank you and good luck.

  2. Jay

    Hey, thanks for the post. I’m working on a programming-bug research project with Utah State University called, “The Bug Theater,” but ran into some bugs with getting a class (I’m very new to jQuery). Realized I forgot the “.” after scrutinizing your tutorial. Thanks for helping me figure out what my bug was! 😉

Leave a Reply


The reCAPTCHA verification period has expired. Please reload the page.