1

I am trying to submit my from using js but onclick is not working when used with input type button, even alert in the function is not working....

<input type="button"  value="Register" id="org_reg_submit" class="reg_input" maxlength="100" onclick="org_reg_submit()">
function org_reg_submit(){
        alert("hi");
        document.getElementById("org_reg").submit();
}
Andreas
  • 21,535
  • 7
  • 47
  • 56

2 Answers2

0

Change the id of the button or the name of the function and the code works since you cannot have the same name of the button and a function

<script>
  function org_reg_submit(){
        alert("hi");
        document.getElementById("org_reg").submit();
}
</script>
<form id="org_reg" onsubmit="alert('form submitted')">
  <input type="button"  value="Register" id="org_reg_submit_button" class="reg_input" maxlength="100" onclick="org_reg_submit()">
  
</form>
mplungjan
  • 169,008
  • 28
  • 173
  • 236
coding-dude.com
  • 758
  • 1
  • 8
  • 27
-2

For anyone who find this in the future:

  • ID not matching in your js function and in your HTML input code. Just match them
  • Ensure the js code is loaded before the HTML one

;)

b1919676
  • 92
  • 2
  • 8
  • This doesn't explain the second part: "_even alert in the function is not working_" – Andreas Jan 23 '17 at 14:51
  • Just ensure the js code is loaded before the HTML and it will work. – b1919676 Jan 23 '17 at 15:04
  • 1
    No, it won't... – Andreas Jan 23 '17 at 15:06
  • Sure it does https://jsfiddle.net/6uz9zn7m/ – b1919676 Jan 23 '17 at 15:08
  • Your fiddle is invalid and also the HTML. If fixed, the order doesn't matter -> https://jsfiddle.net/6uz9zn7m/1/ – Andreas Jan 23 '17 at 15:13
  • Your code simply doesn't work, the alert isn't showing. My jsfiddle is just an example to show that js code must be loaded before the html one, althoug I know the jsfiddle structure is not being 'well used'. – b1919676 Jan 23 '17 at 15:20
  • Exactly. It doesn't work. But that's your code not mine. I've only moved the parts of your "solution" in the correct places... – Andreas Jan 23 '17 at 15:37
  • The problem is not my code (which works) but the structure: js code must be loaded before the HTML one. If the OP just keep this in mind in his own code, it will work, regardless the jsfiddle, which is what I pretended to show – b1919676 Jan 23 '17 at 15:41