0

Here is my code, but it doesn't work (code behind gets empty string): `

<head id="Head1" runat="server">
<title>Pass Javascript Variables to Server</title>
     <script type="text/javascript">             // Second Step
         function f() {
             $("[id$='inpHide']").val("My JavaScript Value");

         }
    </script>
</head>

<body onload="SetHiddenVariable()">
    <form id="form1" runat="server">
    <div>  
        <input id="inpHide" type="hidden" runat="server" />  
        <asp:Button ID="btnJSValue" Text="Click" runat="server" OnClientClick="f"/>
        <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
    </div>
    </form>
</body>

`

3 Answers 3

1

There were couple of issues in your code. The parenthesis were missing while calling f function in onClientClick and if the element has id you can just use the id to select it with #

<head id="Head1" runat="server">
<title>Pass Javascript Variables to Server</title>
     <script type="text/javascript">             // Second Step
         function f() {
             $("input[id*='inpHide']").val("My JavaScript Value");
         }
    </script>
</head>

<body>
    <form id="form1" runat="server">
    <div>  
        <input id="inpHide" type="hidden" runat="server" />  
        <asp:Button ID="btnJSValue" Text="Click" runat="server" OnClientClick="f()"/>
        <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
    </div>
    </form>
</body>
Sign up to request clarification or add additional context in comments.

17 Comments

His input is runat="server". In this example the ClientID will be "inpHide", but in a real world case it may be inside other naming container. I guess that's why he used the end-with syntax $("[id$='inpHide']")
@Nickolay - It was already part of your code snippet. It will execute that method on page load. I have removed it now since you are not aware of that method so need of it.
Looking at his markup it is quite simple but yest if it inside a control or master page it should be client id.
*= will search within the id, it should work fine. Can you check what is the id of this input element when it is rendered on the page.
View source of the page. Right click on the page and just click on view source or view page source whatever option you could see.
|
1

Your <input> tag needs a name attribute, or it won't be posted.

Comments

0

The response from Sean McMillan is right, but I think you also need to change this :

<asp:Button ID="btnJSValue" Text="Click" runat="server" OnClientClick="f"/>

to

<asp:Button ID="btnJSValue" Text="Click" runat="server" OnClientClick="f();"/>

3 Comments

Are you sure your f function is being called? If you put an alert into it?
if after $("input[id*='inpHide']").val("My JavaScript Value"); set alert("something") it doesn't call alert... –
Then your line is causing an error, check the error console in firefox (ctrl+shift+J).

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.