1

When I click on submit1 and then on submit2 everything is going well, but, when I press Enter Key on 1st input text I go to the second part

When I press Enter Key on the 2nd input text -> 1st JavaScript function executes which causes me trouble.

I don't want to disable Enter Key press, but that he executes the good submit input.

Is there a way to deactivate submit1 after he has been executed?

Or know from which input text Enter Key has been pressed?

HTML:

<div id="1">
    <input type="text" placeholder="name"/>
</div>
<div id="2">
    <input type="submit" value="submit" id="submit1"/>
</div>
<div id="3">
    <input type="text" placeholder="firstname"/>
</div>
<div id="4">
    <input type="submit" value="submit" id="submit2"/>
</div>

CSS:

#3, #4
{
    display: none;
}

JavaScript:

$(document).ready(function() {

  $("#submit1").click(function () { 

      /* Verify data with javascript and send it with Ajax */
      /* if everything is ok display: */

      document.querySelector("#2").style.display = "none";
      document.querySelector("#3").style.display = "block";
      document.querySelector("#4").style.display = "block";
  });

  $("#submit2").click(function () { 

       /* Verify data with javascript and send it with Ajax */
  });

});
MatthewMartin
  • 32,326
  • 33
  • 105
  • 164
gr3g
  • 2,866
  • 5
  • 28
  • 52
  • 1
    according to your shared code, the event handler for the second submit is commented out! –  Aug 20 '14 at 16:20
  • on the `submit1` handler create a flag that says you've been there already. then when you click it again, just return. – bitoiu Aug 20 '14 at 16:20
  • I'm new in javascript, could you show me how to do this "flag"? @user3813256 done ;) – gr3g Aug 20 '14 at 16:24
  • 3
    also, you ca disable the button by setting the "disabled" attribute to true via JS. –  Aug 20 '14 at 16:25
  • Is there form tag(s)? – Malk Aug 20 '14 at 16:26
  • All HTML is in 1 `tag` – gr3g Aug 20 '14 at 16:41
  • @user1824508, you should setup a fiddle. It's not clear what you want to happen or what the issue is. I don't see how the code you provided does _anything_ when enter is pressed. Actually, here you go: http://jsfiddle.net/koth/Ltaspofy/ ...it doesn't. – Malk Aug 20 '14 at 17:21

5 Answers5

2

Unless I misunderstood the question - you are simply trying to make sure that the correct event handler gets called based on which button is selected by the user. This will work fine as long as the buttons have unique IDs which they do - and you can associate them with the correct event handler (which it seems like you are doing in the shared code).

Also, you can disable any button using the disabled attribute (set it to true).

to disable document.getElementById("submit1").disabled = true;

to enable: document.getElementById("submit1").disabled = false;

  • Thank you, I didn't know this. This avoids to submit1 when it already has been submitted, but don't submit2 when Enter key pressed on 2nd input text. – gr3g Aug 20 '14 at 16:44
1

If you press ENTER on submit1, submit2 will not be selected unless you hit TAB. Are you doing this?

Anyway, you can do this:

$("#submit1").click(function () { 

      /* Verify data with javascript and send it with Ajax */
      /* if everything is ok display: */

      $("#submit2").focus(); // This will automatically focus the user on the second submit button //
});

This will force the user, the next time he hits ENTER, to submit the submit2 button.

But don't use .submit()... you should use the .submit() function instead of .click(), because I believe .click() only checks for mouse clicks?

$("#submit1").submit(function(){ 
      /* Blah blah blah... */

      $("#submit2").focus(); // This will automatically focus the user on the second submit button //
});

$("#submit2").submit(function(){ 
     /* ... */
});

As other users have said, are submit1 and submit2 in the same <form> tag:

Yes, they were. But you shouldn't have 2 fields in the same <form> tag if you want to submit the data separately.

Do this:

HTML

<form>
    <div id="1">
        <input type="text" placeholder="name"/>
    </div>
    <div id="2">
        <input type="submit" value="submit" id="submit1"/>
    </div>
</form>
<form>
    <div id="3">
        <input type="text" placeholder="firstname"/>
    </div>
    <div id="4">
        <input type="submit" value="submit" id="submit2"/>
    </div>
</form>

JQuery

$("#submit1").submit(function(e){ 
      /* Blah blah blah... */
      e.preventDefault(); // Keeps the user on the same page //
});

$("#submit2").submit(function(e){ 
      /* ... */
      e.preventDefault(); // Keeps the user on the same page //
});

Kenny Worden
  • 4,335
  • 11
  • 35
  • 62
1

From what I can tell your biggest problem here is that you seem to have two submit buttons in a single form tag. I would seriously recommend against this as it can cause issues like the one you are experiencing. Instead I would change both to buttons and add the submit functionality to JavaScript methods as you are kind of doing now.

Obviously though you would want to link the text boxes to a button then and for that I would take a look at this SO question How to trigger HTML button when you press Enter in textbox?

Community
  • 1
  • 1
KHeaney
  • 785
  • 8
  • 18
1

<input type="submit"> is a special control. It will cause the form to submit if the form has focus and the enter button is pressed. When using this you should make use of event.preventDefault() to cancel that behavior when binding to the click event. I suggest using <button type="button"><button> instead.

Malk
  • 11,855
  • 4
  • 33
  • 32
0

I let it work like that:
Why should I have troubles with one form? IE < 6?

HTML:

<form>
<div id="1">
    <input type="text" id="text1"/>
</div>
<div id="2">
    <input type="submit" value="submit" id="submit1"/>
</div>
<div id="3">
    <input type="text" id="text2"/>
</div>
<div id="4">
    <input type="submit" value="submit" id="submit2"/>
</div>
</form>

CSS:

#3, #4
{
    display: none;
}

JAVASCRIPT:

$(document).ready(function() {

  $('#text2').keypress(function(e){
      if(e.keyCode==13)
      $('#submit2').click();
    });

  $("#submit1").click(function () { 

      /* Verify data with javascript and send it with Ajax */
      /* if everything is ok display: */

      document.querySelector("#2").style.display = "none";
      document.querySelector("#3").style.display = "block";
      document.querySelector("#4").style.display = "block";
      document.querySelector("#submit1").disabled = true;
  });

  $("#submit2").click(function () { 

       /* Verify data with javascript and send it with Ajax */
  });

});
gr3g
  • 2,866
  • 5
  • 28
  • 52