views:

50

answers:

3

Hello, I want to disable/enable a button with Javascript. Because the Javascript is called, after a Flash animation is rendered, the button exists at the time of the execution.

The button is in a hierarchy:

<html><body><form#form1><div#control><asp:Button#Export1>

I tried for hours to get a reference to that button, but nothing (document.getElementById("Export1") / document.getElementbyId("form1").getElementById("control").getElementById("Export1") / and many more) seems to work.

How to get a reference to that button (in order to change btnref.disabled = true)?

Thanks a lot for your help!

A: 

If it's the only button in your div, this should work:

var btnref = document.getElementById("controls").getElementsByTagName("button")[0];
peirix
an `asp:Button` renders an `input type="button"`, not a `button`, and that's a vastly unnecessary assumption to make about the DOM *and* a slow means of finding the element, when it does indeed have an id. Any future change of the site, or even a simple greasemonkey script could easily break that approach, for instance.
David Hedlund
+1  A: 

Have you tried right-clicking in the document and selecting "view source" to see how that code actually renders? An asp:Button is a server control, that gets translated to an input field during render. During this, the ID of the field will not be exactly what you set it to in your aspx.

You can use Export1.ClientID at serverside to get the ID of the control.

David Hedlund
This (and the next answer) are working...I did know that, but just totally forgot it -_-Thanks a lot!
powerbar
glad to hear it worked out for you. for the record, for most of us posts are being sorted by votes, and within the same amount of votes, posts are showing up in random order, so there's no way for the rest of us to know what you refer to when you say 'the next answer' =)
David Hedlund
A: 

Usually the id of the button won't stay the same in the page source. Click on view source in the HTML and look for that tag to find the new id. You can then use that id in something like:

document.getElementbyId("Export1_some_unique_id")...
Makram Saleh
if would be preferable not to hard code the id for the very reason that it varies. `document.getElementById('<%=Export1.ClientID%>');` would be one way of doing that. hard-coding the id can cause the script to break for reasons that the user might have thought entirely unrelated to the script, such as upgrading framework versions, which has in some versions come with minor changes in how IDs are being genrated.
David Hedlund