0

I don't understand very well how I get value of with JavaScript or JQuery?

I have this code:

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

In this case I need get value inside the button with id "btnIdButton". I tested some possibilites:

$("#btnIdButton").val()
$('input[id*=btnIdButton]').value()
$("#btnIdButton").text()
document.getElementById("#btnIdButton")

But it did not work.

3
  • 1) ASP.Net webforms changes the id when the button is rendered on the client - you need to get the ClientID property and use that to select the button. 2) The button has no value for you to retrieve, you need to use text() Commented Jul 25, 2016 at 12:51
  • When you say use text would be $("#btnIdButton").text? Commented Jul 25, 2016 at 12:57
  • If you don't want the ID of your button to change, you can set ClientIdMode to static at the control, page, or application levels. Commented Jul 25, 2016 at 13:01

3 Answers 3

2

You need to use "endsWith" selector like this:$("[id$=btnIdButton]")

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

2 Comments

Nice guy, with your example and .text. $("[id$=btnIdButton]").text() it worked!! Thanks!!
This was useful for me as well. Thanks
1
<script type="text/javascript">
    function getASPButtonValue() {
        var btn = document.getElementById("<%=Button1.ClientID %>");
        alert("ASP.NET Button Value= "+btn.value);
    }

    function getHTMLButtonValue() {
        var btn = document.getElementById("btnhtml");
         alert("HTML Button Value= "+btn.value);
     }
</script>

    <div>
        <asp:Button ID="Button1" runat="server" OnClientClick="getASPButtonValue();" Text="Button" />
        <input type="button" id="btnhtml" onclick="getHTMLButtonValue();" value="HTML BUTTON" />
    </div>

Click to see Out Put Screen

Thanks... :)

Comments

0

you can access your button in js $("#<%=btnIdButton.ClientID%>") or use ClientIDMode property of your button

Comments

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.