0

How can I call a Javascript function from a hidden field?

 <asp:HiddenField ID="hdnfield" onChange="callJsFunction()" runat="server" />

So what can replace onChange? because hiddenfield doesn't support onTextChanged...

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Greeed
  • 418
  • 2
  • 8
  • 29
  • 2
    When do you expect `onChange()` to happen for a hidden field. – gideon Mar 02 '12 at 09:21
  • 2
    What client side event makes the hidden field change? – Alberto De Caro Mar 02 '12 at 09:21
  • possible duplicate of [Does HTML Hidden control have any events? Like onchange or something?](http://stackoverflow.com/questions/1003053/does-html-hidden-control-have-any-events-like-onchange-or-something). Also see a related Q: http://stackoverflow.com/questions/2026704/what-event-can-be-captured-when-an-html-hidden-input-value-is-set-changed. Short answer is NO! – VinayC Mar 02 '12 at 09:35

2 Answers2

1

why can not you use

$('#<% hdnfield.Id %>').change( function() { alert("Changed"); })
PraveenVenu
  • 8,217
  • 4
  • 30
  • 39
0

It's tricky to work with hidden fields. Try this listener i wrote. It worked for me in many different cases. I'm using jquery but you don't have to. This one is listening on value change but you can listen on any attribute.

Let say you have hidden input with some initial value:

<input id="change" type="hidden" value="SomeValue" />

Script below will check that value every 2 ses and alert for changes:

// Set empty global var for input value
inputValue = '';

    listenOnChange = function() {

    // Check for new value if any
    checkForNewInputValue = $('#change').val(); 

    if (inputValue == checkForNewInputValue) {

        // Check after 2 sec for change
        setTimeout("listenOnChange()",2000);

    } else {

        // Replace with new value
        inputValue = checkForNewInputValue;

        // Check after 2 sec for change
        setTimeout("listenOnChange()",2000);

        alert('IT WORKS');

    }
}
$(document).ready(
    inputValue = $('#change').val(), // Set Initial Value
    listenOnChange() // Start listener
);

Button below will change that value. Copy, Paste and see how it works.

<button onclick="$('#change').val('1234566');">CHANGE</button>
Webapper
  • 305
  • 2
  • 8