Question

Javascript - find button in nested controls

Asked by: rwallacej

Hi

I have a usercontrol which is displayed either directly on  a master page, or nested on other user controls displayed on a master page. I need some Javascript to find a button on this page. An example of ID may be

ctl00$contentMiddle$MyUserControl1_1$MyUserControl2_1$MyUserControl2_1$theButtonToFind

There will only ever be one theButtonToFind .....but finding it is the problem.

Thanks in advance for Javascript to find the control.  It is is found from a popup window, like this

var theButtonID = "...........theButtonToFind";
var z = window.opener.document.getElementsByName(theButtonID)[0];

                                  
1:
2:

Select allOpen in new window

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2009-11-04 at 07:39:14ID24871184
Topics

JavaScript

,

Programming for ASP.NET

Participating Experts
2
Points
500
Comments
25

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. Nested error
    I have the following code in my file... ___________________________________ <! <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE></TITLE> <SCRIPT LANGUAGE="JavaScript" TYPE="t...
  2. Problem with nested javascript functions... onload
    I have the following onload scripts being executed... I have a problem with nesting the "setfocus" at the end of the statement. Does anyone have a solution to the following? <body leftmargin="0" topmargin="0" marginwidth="0" margi...
  3. ASP.NET 2.0 Beta - Can't get Popup Javascript Cale…
    Hi all, I'm using nested masterpages in my application. For some reason, I can't get my popup javascript calendar to work in the content pages! Any idea why? I am including the javascript file (contains all calendar functions, etc) in the <HEAD> of the main master p...
  4. JavaScript nested IF statement?
    I am new to javascript, but I would assume that there is a way to nest IF statements, correct? I am trying to setup some form validation based upon whether a previous form element was set to Yes. So, in the example below, if Form("PairMe") = "Yes" then I w...
  5. Nested Form Tags and Javascript
    Hi, It appears nested forms do not work.. I have the following piece of code, which I had to simplify inorder to present my case. <form name="aform" method="post" action="newfile.php"> <input type="text" name="abc"...
  6. Javascript nested elements events
    Hello all, I have a <div> tag with a table nested in it. I want to perform a drag and drop funtion on the <div> based upon the div id property. The problem I am having is that when I use the event.getElementById and click inside the table nested in the div the ID...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: wilq32Posted on 2009-11-04 at 07:54:20ID: 25740568

You mean that you know only a part of ID name? or whole name?

1. If you know whole name:

document.getElementById('elementName');

2. If you know only a part of an ID and cant do anything about this:

function findIdContains(name){

var a=document.getElementByTagName('*');
for (var i=0,l=i.length;i<j;i++)
   if (a[i]&&a[i].getAttribute &&a[i].getAttribute('id').test(name)) return a[i];

return null;

}

3. If you can do something about this, then why you dont set a ClassName for it? And use for ex. a jQuery function to select classNamed elements:

$(".className");


 

by: rwallacejPosted on 2009-11-04 at 08:15:27ID: 25740830

Hi wilq32,

I'll try out Option 2 (option 1 won't work as I don't know full name)

R.e. option 3, I haven't worked with JQuery before so wouldn't know where to start with this. Will feed back once Option 2 works

Thanks

 

by: wilq32Posted on 2009-11-04 at 08:20:59ID: 25740899

The option 2 is worst solution :) It means that it goes every all items on page and try to find proper name. It is always better if you can specify tag name (instead of getElementsByTagName('*') use a 'div'  or 'span'), or at least starting point more precisely than a whole document.

There is an error in snippet there:

instead of getElementByTagName type getElementsByTagName

 

by: rwallacejPosted on 2009-11-04 at 08:31:23ID: 25741011

I thought it was worst..but until I get a JQuery hat on it will do
I changed the typo, but a[i].getAttribute('id').test(name))  gives error "property not supported"

 

by: kadabaPosted on 2009-11-04 at 08:47:45ID: 25741242

will this do
a[i].getAttribute('id') == name

test would require a regex

 

by: wilq32Posted on 2009-11-04 at 08:58:21ID: 25741353

you can try:

   if (a[i]&&a[i].getAttribute &&(a[i].getAttribute('id')||"").test(name)) return a[i];

My bad ;)

 

by: rwallacejPosted on 2009-11-04 at 11:32:43ID: 25742967

hi, still got the error with code

(a[i].getAttribute('id') || "").test(name)      

Object doesn't support this property or method      Error

 

by: kadabaPosted on 2009-11-04 at 11:40:46ID: 25743050

did you try my assist?

try using match and see
(a[i].getAttribute('id') || "").match(name)    

 

by: wilq32Posted on 2009-11-04 at 11:50:57ID: 25743168

omg sorry my fault...

function findIdContains(name){

var a=document.getElementByTagName('*');
var b=new RegExp(name);
for (var i=0,l=i.length;i<j;i++)
   if (a[i]&&a[i].getAttribute &&(b.test(a[i].getAttribute('id') || "")) return a[i];

return null;

}

 

by: wilq32Posted on 2009-11-04 at 11:51:07ID: 25743170

ElementsByTag ... uff

 

by: kadabaPosted on 2009-11-04 at 11:55:20ID: 25743218

yes I did mention

"test would require a regex" :)

 

by: wilq32Posted on 2009-11-04 at 11:57:44ID: 25743249

First idea was to use .match  but its really unperformant then switched to test but I do sometimes forgot that .test got oposit direction of testing regexp stuff... uff i'm overworked recently damn..

 

by: rwallacejPosted on 2009-11-04 at 13:04:41ID: 25743909

I've ended up going for the option 3 after all, entered into JQuery

            // find the button
            var ae = $("[id$=theButtonToFind]").attr("id");
            var theRefreshBtn = document.getElementsByName(ae)[0];
            theRefreshBtn.click();

 

by: rwallacejPosted on 2009-11-04 at 13:18:12ID: 25744040

this works when control is on same page, not on the parent window..need to work out how to get it on parent window now

 

by: wilq32Posted on 2009-11-04 at 14:41:45ID: 25744863

$("[id$=theButtonToFind]",parent)   second argument is to define a scope to look in

 

by: rwallacejPosted on 2009-11-04 at 15:12:43ID: 25745146

thanks for more help....I'm not sure if nearly there yet.....see javascript below of what is happening /what trying to do

part 1 (hardcoded full string of parent works) - javascript find button and executes code-behind
part 2a does nothing at click. The debug gives [Object] [Object]
part 2b throws error

almost there I hope

// part 1
// successfully calls "Click" code-behind on parent
alert("btnID click");
var btnID = "aVeryLongStringOfUserControls_____xxx____theButtonToFind";
btnID = window.opener.document.getElementsByName(btnID)[0];
btnID.click();
 
 
// part 2a
alert("theRefreshBtn");
var theRefreshBtn = $("[id$=theButtonToFind]", parent);
theRefreshBtn.click();      // does nothing, no postback and no errors
 
// part 2b
alert("theButtonToFind2");
var theRefreshBtn2 = theRefreshBtn.attr("id");
alert(theRefreshBtn2);           // undefined
alert("theRefreshBtn2 click");
theRefreshBtn2.click();            // error
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:

Select allOpen in new window

 

by: wilq32Posted on 2009-11-04 at 15:26:28ID: 25745278

ok so first of all what jQuery returns is not a DOM object, but more like jQuery array of objects (you could say).

Function .click(func)  in jQuery mean "attach event onclick to all items selected using jQuery selector, fire a func on that event".


Can you try do something like this:

var theRefreshBtn = $("[id$=theButtonToFind]", parent);
theRefreshBtn.each(function(){

   alert($(this).attr('id'));

});


then we see if it finds anything that might be usefull for you

 

by: rwallacejPosted on 2009-11-04 at 23:43:57ID: 25747428

hi, the attached gives no alert messages
var theRefreshBtn = $("[id$=theButtonToFind]", parent);
theRefreshBtn.each(function(){

   alert($(this).attr('id'));

});

 

by: rwallacejPosted on 2009-11-04 at 23:44:23ID: 25747431

further help very much appreciated

 

by: wilq32Posted on 2009-11-05 at 02:08:18ID: 25748008

based on http://docs.jquery.com/Core/jQuery#expressioncontext can you try different approaches with top.window,  top ? no results at all ?



var theRefreshBtn = $("[id$=theButtonToFind]", top.window);
theRefreshBtn.each(function(){

   alert($(this).attr('id'));

});

 

by: rwallacejPosted on 2009-11-05 at 03:54:37ID: 25748536

still no joy, neither of following give any alerts

        var theRefreshBtn = $("[id$=btnRefreshListOfItems]", top.window);
        theRefreshBtn.each(function() {
            alert($(this).attr('id'));
        });

        var theRefreshBtn = $("[id$=btnRefreshListOfItems]", top);
        theRefreshBtn.each(function() {
            alert($(this).attr('id'));
        });

(I have typed btnRefreshListOfItems correctly)

do you have any more suggestions?

 

by: rwallacejPosted on 2009-11-05 at 04:03:59ID: 25748584

solution eventually!

        var theRefreshBtn4 = $("[id$=btnRefreshListOfItems]", window.opener.document );
        theRefreshBtn4.each(function() {
            //alert("trial 5...here!");
            //alert($(this).attr('id'));
            var btnID = $(this).attr('id');
            var btn = window.opener.document.getElementsByName(btnID)[0];
            btn.click();
        });

 

by: wilq32Posted on 2009-12-02 at 08:09:16ID: 25953286

Well eventually author did get solution, based on experts input, but with own research added. Would be best if he decides, if not then split should be fine ?

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...