1

I am trying to pass an asp.net button control's id to a javascript function. When I call the js function start_time, the text of the button needs to change. I have done a lot of research and Im still not able to get it to work. Any help or advice is much appreciated. Below is the code I have.

<asp:Button ID="btnStart" runat="server" Text="OK" />

            $("#btnStart").click(function () {
            start_time("btnStart");
        });

            function start_time(controlStart) {
            var a = document.getElementById(controlStart);
            a.innerHTML = "Start";
        }
4
  • This should help.. stackoverflow.com/a/19860904/489512 Commented Nov 8, 2013 at 16:55
  • asp.net server control's ID will change when rendered because of runat="server". So can't use $("#btnStart").. Commented Nov 8, 2013 at 16:56
  • What is version of .NET framework? Commented Nov 8, 2013 at 17:03
  • .NET Framework is 4.5 Commented Nov 8, 2013 at 17:22

3 Answers 3

1
<asp:Button ID="btnStart" ClientIDMode="Static" runat="server" Text="OK" />

$("#btnStart").click(function () {
    start_time("btnStart");
});

function start_time(controlStart) {
    var a = document.getElementById(controlStart);
    a.innerHTML = "Start";
}

If you set ClientIDMode1 to static, it should work. http://msdn.microsoft.com/en-us/library/system.web.ui.control.clientidmode(v=vs.110).aspx

Sign up to request clarification or add additional context in comments.

Comments

0

The easiest way I've found to select ASP.net controls at the client is like this...

$("[ID$=btnStart]").click(function() {
    start_time(this);
});

In this code you pass the button into the function start_time, so you don't need to search the DOM for it...

function start_time(control) {
    control.innerHTML = "Start";
}

Due to the prefixing of IDs that ASP.net does to controls, the ID can change from page to page, in different contexts. That selector basically selects every element whose ID ends with btnStart.

There are other ways, using ClientID for example, but that can mean mixing ASP with JS files, depending on where your script is actually running.

If this script is running directly on the page, rather than from an included js file then you can do the following...

$("#<%= btnStart.ClientID %>").click(function() {
    start_time(this);
});

That will select the button by the ID it is given when the page is parsed on the server.

4 Comments

I'd be very interested to know why this was down-voted, since it's correct.
Thanks @RayCheng. There's been a bit of that going on recently :)
I've tried all the above solutions and I still cant get it to work. Strange!
Are you putting the javascript inside a jQuery document.ready handler?
0

@Archer answer is almost correct but you should use ClientID like this..!!

 $('#' + '<%= btnStart.ClientID %>').click(function() {
start_time(this);
});

3 Comments

This should be a comment, not asnwer.
i didnt find any add comment link..!! So I had posted it as answer..!! I am only able to comment under this answer..!! I don't know why ??
Also, this will be slightly different from my answer, in output, but will have exactly the same effect. Re-check my code :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.