views:

49

answers:

3

Hello,

"I have html that has 2 divs with the same id's in jquery i have it select the containers using $('#idname') in IE8/firefox/chrome it changes the html on both divs with that same id, but in ie 7 and below it only changes 1, how can i make this compatible with ie7 to select all divs with id? I would use classes but i already have a class that each of these div's share the same class name. Is there a div tag i can use for this other than id ?"

Ok i understand div's id should be unique, is there any other tag i can use other than title because it leave the tooltip mouseover on the div, that will allow me to select the value in jquery?

Thanks

+1  A: 

I have html that has 2 divs with the same id's in jquery i have it select the containers using $('#idname') in IE8/firefox/chrome it changes the html on both divs with that same id

Ids can not be same, an id should be unique per element per page. You need to use their common classes and modify your jquery code as per that or use some other surrounding elements to target your desired elements.

Sarfraz
ok understood, so for instance say i have every product div with a class="product" and i use jquery to $('.product').each(function(){}); How would i then add another class and have jquery check the second class instead of product?
William Smith
you can do $('.product.otherClass').each(function(){});
Patricia
@Patricia: Exactly as far as we could understand it :)
Sarfraz
Just a minor semantic quibble, but an `id` should be **unique per page**. (Unique per element per page sort of suggests that a `ul` and a `div` could both have an `id` of 'navigation' (for example).)
David Thomas
A: 

You can have multiple classes per element, but you can't have multiple elements with the same ID. Just put a space-separated list into the class attribute.

<div class="product foo"></div>

That div has two classes, "product" and "foo". Now your jQuery can do this...

$('.foo').each(...);

Or if you want to process only elements that have both "product" and "foo" classes, you can do this:

$('.product.foo').each(...);

If you want to do elements that have either "product" or "foo" classes...

$('.product, .foo').each(...);
Joel Mueller
Hey joel, gotcha on that ok say i wanted <div class="product product1"></div> <div class="product product1"></div><div class="product product2"></div> and so on how would i retrieve the product1 from the class using jquery?
William Smith
Just do `$('.product1')` to select everything that has the "product1" class, regardless of what other classes might be on that element.
Joel Mueller
Ok heres my issue .product1 or .product2 is dynamic. so it may be .product454 on this load 234 on the next load
William Smith
Perhaps your server-side code that generates `.product454` could also generate some client-side javascript that selects that class? You could make a JS function that takes the class name as a parameter, and then plugs it into `$('.' + theClassName)`
Joel Mueller
A: 

I added custom html tags to the elements i needed to

<div customtag="infohere">
stuff here!
</div>

and used this jquery to select it

$('div[customtag='"+product+"']')

product being a variable with the id number of the element.

William Smith