javascriptasp.netbuttononclickonclientclick

OnClientClick Javascript:confirm get value in textbox?


So I can't seem to find anywhere how to do this. I want to be able to use the value of BuyOutPrice in the text(confirm) box but can't seem to make it work.

More specificly, how am I supposed to write this part: ('Are you sure you want to buy-out for $' + BuyOutPrice + '?')

This was just my closest guess from experience in other programming languages but it appears invalid in JavaScript.

var BuyOutPrice = '<%= Content.ComparisonPrice %>';


<asp:ImageButton ID="BuyNowButton" OnClick="BuyNowButton_Click" Style="vertical-align:top;" ImageUrl="Images/btn_buyNow.png" runat="server" OnClientClick="javascript:return confirm('Are you sure you want to buy-out for $' + BuyOutPrice + '?'); BuyNow(); return ValidateBuyNow();" />

Solution

  • If ComparisonPrice is a TextBox so try this:

    <asp:TextBox ID="ComparisonPrice" runat="server"></asp:TextBox>
    <asp:ImageButton ID="BuyNowButton" Style="vertical-align:top;" ImageUrl="Images/btn_buyNow.png" runat="server" OnClientClick="javascript:return confirm('Are you sure you want to buy-out for' + momo() + '?'); BuyNow(); return ValidateBuyNow();" />
    

    And JavaScript:

    <script>
        function momo() {
            return document.getElementById('<%= ComparisonPrice.ClientID %>').value
        }
    </script>