tags:

views:

40

answers:

3

i have two divs with the same ids but one has display=none and the other has display=block i want to delete the one with display='none' i am using javascript framework prototype

+2  A: 

You shouldn't have two elements with the same ID. You should use classes.

metrobalderas
+4  A: 

You cannot have two controls with the same ID, it is invalid. You should rewrite your code so the IDs are not the same.

SLC
i am generating html by ajax so its possible
vakas
@vakas, regardless of whether it's possible, it's *wrong*. It causes all sorts of problems like the one you're having now. You need to change whatever you're using to generate the html so that it doesn't create two elements with the same ID.
tloflin
no it's not. If your code adds elements with the same ID than you should fix that as it is invalid.
Pim Jager
This is indeed "advice" but not an answer, you should vote up @Pim Jager's answer, it is the only one that provides advice and a solution.
@clarke78 - I think the responders are trying to teach the importance of keeping IDs unique. Arguably this concept is so important that answers to the question posed are moot (and possibly misleading).
Upper Stage
I think the responders are also getting restless that this is the third near-identical question and the OP has repeatedly ignored the vital “don't reuse IDs” advice.
bobince
+2  A: 

You should switch to giving the divs classes instead of id's. ID's are unique identifiers, thus are unique, and a page should only contain one element per id (an id can not have multiple elements).

If you'd gave the divs classes instead of id's you could do this:

$$(".<your_class_here>").reject(Element.visible).each(Element.remove);

My Prototype is a bit rusty, but I think this should do it.

EDIT: forgot you can't directly filter by CSS properties.

Pim Jager
m trying thisvar temp=$$(".the_div[display='none']");alert(temp.length);but dis alerts nothing <div id="2" class="the_div" style="display:block"> its testing </div> <div id="2" class="the_div" style="display:none"> its hidden</div>
vakas
its working... great thanx...
vakas
@vakas: Not only are you re-using `id`​s, your `id` isn't even a valid name (you can't start with a number). **Seriously, you need to fix this** or you are going to get many strange browser misbehaviours.
bobince