I would like to add JavaScript code that whenever a user types the Enter button into a specific text box it will click on a specific button on the page.
@toicontienMay 12.2009 — #Something like this might help: [CODE]function associateField(field, buttonName) { field = (typeof(field) == "string") ? document.getElementById(field) : field; buttonName = buttonName || field.name + "_button"; return function(e) { if (e.keyCode == 13) { this.form.elements[buttonName].click(); return false; } }; }[/CODE] And to use it: [CODE]// Get node reference to <input type="text" name="foo" id="my_text_box"> var el = document.getElementById("my_text_box");
// Case A: el.onkeypress = associateField(el, "button_name");
// Case B: el.onkeypress = associateField(el); // Button name is assumed to be "foo_button"[/CODE] And the HTML: [CODE]Case A: <input type="text" name="foo" id="my_text_box"> <input type="submit" name="button_name">
Case B: <input type="text" name="foo" id="my_text_box"> <input type="submit" name="foo_button">[/CODE]
The keyCode you need to detect is 13, which is the code for the ENTER key.