views:

90

answers:

3

Suppose I have these divs:

<div class="hat" rel="cap">
<div class="hat" rel="pine">
<div class="hat" rel="mouse">

How do I use JQuery to do a "where"?

For example

$("div.hat").remove WHERE rel="mouse"
+7  A: 

Use the Attribute equals selector:

$('div.hat[rel=mouse]').remove()
Victor Nicollet
+4  A: 

You should be able to use the attribute selector:

$("div.hat[rel]") // to get all <div class="hat"> tags with a rel attribute

$('div.hat[rel="mouse"]') // to get a <div class="hat"> tag with a specific rel attribute
Ian Oxley
+1  A: 

Use one of the attribute selectors such as the Attribute Equals Selector for this type of selection. For example:

$('div.hat[rel=mouse]').remove();

There are a number of other variations of the attribute selector to do things like matching an element whose attribute begins with, ends with, or contains a certain value. Check out all the Attribute Selectors at the jQuery API and familiarize yourself with them.

Jimmy Cuadra