MarMan29 MarMan29 - 1 year ago 84
jQuery Question

first-child without certain class

I'm trying to select the first element under id #pid which contains the .pe class but not the .break class. Then obviously get a certain attribute from that element. I have tried different variations the following doesn't seem to work:

$("#pid .pe:not(.break):first-child").attr("data-time");

neither does :

$("#pid .pe:first-child").not(".break").attr("data-time");

Can someone please tell me what's wrong with my logic.


Answer Source

The problem is with the select of the first child.

This should look like this:

alert($("#pid .pe:not(.break)").first().attr("data-time"));

Or as per Jay's suggestion -

$('#pid .pe:not(.break):first').attr('data-time');

Fiddle example

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download