jQuery – How to get element with CSS class name and id
Published: May 6, 2010 , Updated: May 4, 2010 , Author: mkyong
In jQuery, you can get elements with CSS class name and id easily.
For example,
1. ID: #id
- $(‘#idA’) – selects all elements that have an id of ‘idA’, regardless of its tag name.
- $(‘div#idA’) – selects all div elements that has an id of ‘idA’.
2. Class: .classname
- $(‘.classA’) – selects all elements that have an class name of ‘classA’, regardless of its tag name.
- $(‘div.classA’) – selects all div elements that has an class name of ‘classA’.
Full Example
<html> <head> <title>jQuery Get element with class name and 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>
Any Java questions or problems? please post at this JavaNullPointer.com forum, see you there ~
[ Read More ] You can find more similar articles at jQuery Tutorials
How to select the element with both #id and .classname in one go.
If there is like this:
Now I want to select div with id #id_1 and class .simple what should I do?
In HTML, “id” should be unique in your page.
[...] Select CSS class name and id with jQuery Select the CSS class name and id with jQuery, $(.classname) and $(#id). [...]