Login Register

relative position query

Hi,
if in a page there are two element with same id, dojo query with relative position don't work properly.

i.e.

	
		testing dojo.query()
		
			@import "../../resources/dojo.css";
		
		
		
			dojo.addOnLoad(function() {
		         dojo.query("#b3", "thisForm").forEach(function(n){n.value="X";}); // <--- !!!! 
    		});
    
	
	
		

testing dojo.query()

when the page execute the first input element in body as value "X" and not the element inside the form.
I have tested with 1.0 and 1.1.0 b2.

Thanks in advance

You shouldn't have two id's

It's a pretty fundamental principle that ID's should be unique.

yes but...

yes ID should be unique but in my real project i load the form by xhr and it's not possible to know if there are element with the same id.

Alternative solution...

Give them both a classname and then query on that if you're trying to hit both.

i don't want both

unfortunately i don't want to hit both element but only that inside form.

dojo.query(".elClassO")[0]

dojo.query(".elClassO")[0] is the first one

it goes back to "not using duplicate ID's" ... if you can't ensure that a snippet will ever only appear once on a page, don't give it an id ... give it some unique class, and search for it.

if you know the form the query is in (form id="foo")

dojo.query(".elClass0","foo").forEach(function(n){  ... });

if you have a dom like:

you can get your input like:

dojo.query("div.test .elClass0")

best solution for me...

I reflect on dante solution and it works very well.
This is the "final" working code:

    
       
   

and i use classname to retrieve the node:

dojo.query(".foo", "thisForm");

This works, thanks.

P.S.
I don't know the implementation of dojo.query but if i specified a relative position to execute a query i'm expected that only this portion of page are processed and not all page, it's true?