views:

41

answers:

2

<div id="id" class="div-style"><img id="id" class="img-style"></div>

I would like to use the id attribute as a way for jQuery to select the element, and the class attribute for CSS to style it.

My guess for jQuery selector ran thusly:

$("#id .div-class")

and

$("#id .img-class")

This returns the error "Selector expected"

+1  A: 

The id attribute should (as per HTML standard specifications) be unique across all elements in a page. Thus, if that <div> is the only element in your page with the id attribute set to "id", then you can safely use $("#id").

Felix
+1  A: 

Using

$("#id .div-class")

and

$("#id .img-class")

attempts to select descendent elements with CSS class 'div-class' and 'img-class' of the element with id id, respectively, which won't return anything. Selecting an element by id is the fastest way and you wouldn't need to specify the CSS class if using id, since the HTML specification states that elements ids should be unique within a page. So, using just

$("#id")

will select the div with id id. Now what would you like to do with the element?

Russ Cam
I get it, thanks.
ben