Solved

Need to enable an ASP.NET button using JavaScript

Posted on 2008-10-25
6
1,438 Views
Last Modified: 2013-12-17
I can successfully change the image displayed on a buton with this JavaScript code:

                       var button = document.getElementById("findReservationBtn");
                        button.src="images/btn_forward_flow.gif";

But I also need to disable and enable it, and am having trouble.

Can anybody help do it?

thanks,
newbieweb
0
Comment
Question by:newbieweb
  • 4
  • 2
6 Comments
 
LVL 12

Expert Comment

by:techExtreme
ID: 22804279
Hello,
Going with your existing code, assuming you have the imagebutton's reference in the button variable (i.e. it is not null first of all),
do this:
button.disabled = 'true';  \\for disabling the button
button.enabled = 'true'; \\ for enabling the button
Hope it helped,
Happy Coding!
0
 
LVL 12

Expert Comment

by:techExtreme
ID: 22804296
err I guess a type, both ways you should use the disabled property only.
0
 

Author Comment

by:newbieweb
ID: 22804345
I tried

button.disabled='false' and
button.disabled='true'

and for some reason I can disable the button but can not enable it this way:

button.disabled='false'

Anny ideas?
0
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 
LVL 12

Expert Comment

by:techExtreme
ID: 22804474
Hi,
I guess you are trying to enable the same button which you disabled by clicking on itself.
Say you have a button1, you disable it by setting its disabled property to true. Now when you cannot click it again and execute its code as it is already disabled. You must have another method of enabling the button you just disabled,

say from another button click or any other event  you set the disabled button's property to false and it will be enabled.
For understanding purposes
I have two buttons: button1 and button2
on button1's click , i disable it by button1.disabled = 'true';
Now ,
to enable buton1, i use button2's click event by firing below code there
button1.disabled = 'false';
Did this help? Let me know.
Thanks.
0
 

Author Comment

by:newbieweb
ID: 22804534
I am not trying to enable or disbable it from the button itself.  I am intercepting keystrokes.  So I know that the button is not getting re-enabled even though the JavaScript is being called.  I can see the image file changing properly and you can see in the code below hte proximity to the code which changes the image and enables and disable the button.

Do you see what I mean?

newbieweb

function CheckSize()
    {
        var elem = document.getElementById("recordLocatorTextBox");
       
        if (elem != null)
        {
            if (!isAlphaNumeric(elem.value))
            {
                alert("The confirmation number consists only of alphanumeric characters.  Please try again.");
            }
            else
            {
               var button = document.getElementById("findReservationBtn");
               number_of_characters = parseInt(elem.value.length);
               if(number_of_characters == 6)
                {
                        //display enabled button image
                        if ( button != null )
                        {
                            button.src="images/btn_forward_flow.gif";
                            button.disabled = 'false';
                        }
                        return true;
                }
                else
                {
                    if (number_of_characters>6)
                    {
                        alert("The confirmation number is only 6 characters long.  Please try again.");
                    }

                    // display disable button image
                    if ( button != null )
                    {
                        button.src="images/btn_forward_flow_dis.gif";
                        button.disabled = 'true';
                    }            

                    return false;
                }
            }
        }
   
0
 
LVL 12

Accepted Solution

by:
techExtreme earned 500 total points
ID: 22804726

Hello,
It works, just remove the quotes from 'true' and 'false'
i.e. use button.disabled = true; and button.disabled = false; and it will work perfectly.
As an example, I'm posting a html test form that i used just now ,
if removing quotes does not work for you, copy and paste below code in notepad and save as test.html and
check by clicking both buttons and effect of it in image.It works here.

<html>
<body>
<form id="form1" action="http://www.google.com" method="GET">
<input type="image" id="img1" type="image" src="http://www.bannermakerpro.com/images/sample6-greenpill.gif">
<a href="#" onclick="javascript: e()">enable</a>
<br />&nbsp;
<br />
<a href="#" onclick="javascript: d()">disable</a>
</form>
<br />
</body>
</html>

<script type="text/javascript">
function e()
{
 document.getElementById('img1').disabled=false;
 alert('enabled');
}
function d()
{
 document.getElementById('img1').disabled=true;
 alert('disabled');
}
</script>
Thanks,
Enjoy Coding
0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

685 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