Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does HTML Hidden control have any events? Like onchange or something?

Can I attach any event handlers to HTML hidden input fields? Basically I want to run a function when a hidden input field value changes.

like image 522
Hcabnettek Avatar asked Jun 16 '09 18:06

Hcabnettek


People also ask

What is hidden control in HTML?

The <input type="hidden"> defines a hidden input field. A hidden field lets web developers include data that cannot be seen or modified by users when a form is submitted. A hidden field often stores what database record that needs to be updated when the form is submitted.

What is Onchange in HTML?

Definition and Usage The onchange attribute fires the moment when the value of the element is changed. Tip: This event is similar to the oninput event. The difference is that the oninput event occurs immediately after the value of an element has changed, while onchange occurs when the element loses focus.

How do you add Onchange to HTML?

To add background color in HTML, use the CSS background-color property. Set it to the color name or code you want and place it inside a style attribute. Then add this style attribute to an HTML element, like a table, heading, div, or span tag.


2 Answers

Events are only triggered when the user performs the event in the browser, so if it's <input type="hidden"> or an <input> hidden by CSS, the user won't be able to trigger events to your input.

The only way you would get onchange to work is if you manually trigger onchange in Javascript. A quick example of this:

<form name="f" onsubmit="document.f.h.value='1';                            document.f.h.onchange();                            return false;"  >      <input type="hidden" name="h" value="0" onchange="alert(document.f.h.value);" />      <input type="submit" />  </form>
like image 115
jimyi Avatar answered Sep 23 '22 01:09

jimyi


Yes, certain browsers (such as Firefox) will fire an onclick or onfocus event on hidden elements when they're activated via an accesskey attribute (meant to provide a keyboard hotkey to jump to an input).

Open the below in firefox, focus the frame, then strike Alt+Shift+x (Windows and Linux) or Ctrl+Alt+x (Mac).

<input type="hidden" accesskey="x" onclick="alert('clicked!');" />
like image 20
jkingsman Avatar answered Sep 20 '22 01:09

jkingsman