[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Enabling   buttons from Javascript

Posted on 2012-08-29
2
Medium Priority
?
436 Views
Last Modified: 2012-08-30
I need to enable 3 buttons when the value of a drop down is changed.  The Javascript is

function enablebuttons(ddlStatus) {

            var button1 = document.getElementById('<%= btnAddItem.ClientID %>');
            var button2 = document.getElementById('<%= btnsavee.ClientID %>');
            var button3 = document.getElementById('<%= btnsaven.ClientID %>');
           

            if (ddlStatus.options[ddlStatus.selectedIndex].value != 2) {
                button1.disabled = false;
                button2.disabled = false;
                button3.disabled = false;
               

            }
        }

When this executes the buttons look like they are enabled, but they are not clickable.  Does anyone have any ideas how to make the buttons clickable.
0
Comment
Question by:sherbug1015
2 Comments
 
LVL 10

Expert Comment

by:Gregg
ID: 38348252
Have you defined a click event for the buttons?
0
 
LVL 16

Accepted Solution

by:
Vikram Singh Saini earned 2000 total points
ID: 38348733
I tested your requirements. And here is the code for same:

Default.aspx:
<%@ Page Language="C#" AutoEventWireup="true"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Enable buttons from JS</title>
    <script type="text/javascript" language="javascript">
        function enablebuttons(ddlStatus) {

            var button1 = document.getElementById('<%= button1.ClientID %>');
            var button2 = document.getElementById('<%= button2.ClientID %>');
            var button3 = document.getElementById('<%= button3.ClientID %>');            

            if (ddlStatus.options[ddlStatus.selectedIndex].value != 2) {
                button1.disabled = false;
                button2.disabled =false;
                button3.disabled = false;
            }
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div align="center">
        <h3>
            Enabling buttons</h3>            
        <asp:DropDownList ID="ddlIndex" runat="server">
            <asp:ListItem>1</asp:ListItem>
            <asp:ListItem>2</asp:ListItem>
            <asp:ListItem>3</asp:ListItem>
        </asp:DropDownList>
        <hr />
        <asp:Button ID="button1" runat="server" Text="Hello" Enabled="false" />
        <asp:Button ID="button2" runat="server" Text="Hello 2" Enabled="false" />
        <asp:Button ID="button3" runat="server" Text="Hello 3" Enabled="false"/>
        <hr />
        <input type="button" onclick="enablebuttons(ddlIndex)" value="Enable" />
    </div>
    </form>
</body>
</html>

Open in new window


As you can notice we have used html button, not the asp button. If used asp button for calling onclientclick function, the asp:button makes postback, so all fields get reinitiated to their previous saved state in viewstate.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
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
Course of the Month17 days, 14 hours left to enroll

829 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