Solved

Need to enable an ASP.NET button using JavaScript

Posted on 2008-10-25
6
1,439 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Suggested Solutions

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…

738 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