Solved

Access a dynamically created element

Posted on 2011-02-14
10
644 Views
Last Modified: 2012-05-11
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.
0
Comment
Question by:cerub
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
10 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 34887616
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
 

Author Comment

by:cerub
ID: 34887744
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
 
LVL 82

Expert Comment

by:leakim971
ID: 34887840
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:cerub
ID: 34887873
"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
 
LVL 82

Expert Comment

by:leakim971
ID: 34887990
var myRef = document.getElementById("theDivIdIKnow").childNodes[1];

Open in new window


try 0 instead 1 too
0
 
LVL 82

Assisted Solution

by:leakim971
leakim971 earned 250 total points
ID: 34888082
another one, use getElementsByTagName("*")[0] instead childNodes[1]
0
 

Author Comment

by:cerub
ID: 34894616
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
 
LVL 2

Accepted Solution

by:
Aazmodeus earned 250 total points
ID: 34895976
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
 

Author Closing Comment

by:cerub
ID: 34896044
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
 
LVL 82

Expert Comment

by:leakim971
ID: 34896053
>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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
asp.net web application 3 57
Error running webpage local computer 7 35
Header Font Size in Grid View 6 45
Get Pop-Up Or Div Info Box From JQuery DataTable Cell in ASP 7 33
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question