Solved

Need to enable an ASP.NET button using JavaScript

Posted on 2008-10-25
6
1,431 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
Comment Utility
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
Comment Utility
err I guess a type, both ways you should use the disabled property only.
0
 

Author Comment

by:newbieweb
Comment Utility
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 12

Expert Comment

by:techExtreme
Comment Utility
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
Comment Utility
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
Comment Utility

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
C3-->D3 Line Chart 4 16
.Net application crashing 6 18
Align Buttons next to each other 2 10
JavaScript error 1 31
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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…

728 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now