Access a dynamically created element

Hello,

We need to access dynamically created elements on the client side. We would like to do that by using their IDs.

Currently, we can achieve this via the Sys.Application._components node.

Any other viable suggestions?

$Find can be used but we don't know the ID of the elements.

getElementsByTagName wouldn't yield the right names since the objects themselves aren't rendered (CollapsedPanelExtender).

What other means are there to access the right ID other than using Sys.Application.getComponents() ????

Thanks a bunch.
cerubAsked:
Who is Participating?
 
AazmodeusConnect With a Mentor Commented:
If you create components dynamically, I guess, you can set their IDs and pass them as parameters to a view. I don't really remember how to do that in ASP.NET. But in MVC you can add them into page Model or into ViewData dictionary and then call a javascript function using the passed parameters. Or you can simply create your own class where these ids will be defined as constants, which should be legit for both plain ASP.NET and MVC.

<script>
 //id is passed as a field DynamicElementID of view Model
 var obj = document.getElementById('<%=Model.DynamicElementId%>');
 //id is defined as a constant or a static field in ViewParams class
 var obj = document.getElementById('<%=ViewParams.DynamicElementId%>');
</script>

Open in new window

0
 
leakim971PluritechnicianCommented:
If you put it in a unique "container", for example a div with a specific ID that you know, you know the child of this div is your object.
0
 
cerubAuthor Commented:
But wouldn't the child object's ID change in an arbitrary way as well? I would have to somehow retrieve the "new" ID of the DIV as well. What do you think?
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
leakim971PluritechnicianCommented:
I don't you really need an ID. Generaly we need to get a référence on an object. Once you get this reference you can get its ID easily. The div should be in your page. You know its ID. You put your dynamic object inside.
0
 
cerubAuthor Commented:
"Generaly we need to get a référence on an object. Once you get this reference you can get its ID easily."

Could you possibly provide an example reference, please?
0
 
leakim971PluritechnicianCommented:
var myRef = document.getElementById("theDivIdIKnow").childNodes[1];

Open in new window


try 0 instead 1 too
0
 
leakim971Connect With a Mentor PluritechnicianCommented:
another one, use getElementsByTagName("*")[0] instead childNodes[1]
0
 
cerubAuthor Commented:
Don't get me wrong but you still don't seem to know the names of the elements. You iterate them in order to learn their names.
0
 
cerubAuthor Commented:
Thank you! It seems there are two main ways of looking at this problem so I think it is only fair to split the points.

Thanks for the help.
0
 
leakim971PluritechnicianCommented:
>Don't get me wrong but you still don't seem to know the names of the elements.
The question is : Do you really need the name ? Or do you need a reference to work with the objects ?

>You iterate them in order to learn their names.
No iteration in my code, we get the first node having a tag... and I'm not sure a function like $Find don't use in its black chamber one loop (or more) or something not linear.

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.