Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery selector for div with class

Tags:

I tried this below. I think the return Object of $("div.tab_select")[0] isn't a jQuery Object, but I can't even use pure javascript method.

Is there any way to make it jQuery Object? for instance $($("div.tab_select")[0])..I know this's silly;

Thank you for reading.

var tmp = $("div.tab_select")[0]; 
alert(tmp); //This gives me HTMLDivElement collectly. But I can't use any of javascript..

alert(tmp.nodeName); //But this give me error "Uncaught TypeError: Cannot read property 'nodeName' of undefined"

tmp.hide(); //Neither, I can't use this.
like image 566
Patrick Jeon Avatar asked May 11 '12 19:05

Patrick Jeon


People also ask

How do I select a div with a specific class?

In one of them, we will use a method and in the other one, we will use not selector. Approach 1: First, select the DIV with certain class using jQuery Selector and then use :not selector to ignore the elements of specific class. Example: This example implements the above approach.

How do I select a specific class in jQuery?

In jQuery, the class and ID selectors are the same as in CSS. If you want to select elements with a certain class, use a dot ( . ) and the class name. If you want to select elements with a certain ID, use the hash symbol ( # ) and the ID name.


3 Answers

// all divs with tab_select class
$('div.tab_select')

// first div with tab_select class
$('div.tab_select:first')

// or CSS pseudo selector which is slightly faster than the first jQuery 
// shortcut 
$('div.tab_select:first-of-type')

// or
$('div.tab_select').first()

// or
$('div.tab_select:eq(0)')

// or
$('div.tab_select').eq(0)
like image 111
jmar777 Avatar answered Sep 22 '22 15:09

jmar777


if you want a jQuery object use var tmp = $("div.tab_select:first") instead.

var tmp = $("div.tab_select")[0] will return the DOM element (if exists)

like image 29
Sagiv Ofek Avatar answered Sep 25 '22 15:09

Sagiv Ofek


Just do $(tmp). [0] gives you the HTML-Element not the JQuery instance.

like image 1
dav1d Avatar answered Sep 23 '22 15:09

dav1d