Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Need to enable an ASP.NET button using JavaScript

Posted on 2008-10-25
6
Medium Priority
?
1,444 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 2000 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

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

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…
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
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…
Suggested Courses

926 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