1

I made a expand form after button press, but now I want to add Enter key press too, but I don't know how. Maybe someone can help me!?

CODE

$(document).ready(
    function() {
    $(".form-fieldError").hide();
        $("#expand-form-button").click(function() {
            if( document.getElementById('sign_up_email').value === '' ){
                $(".form-fieldError").show(1);
            }else{
                $("#form-expand").fadeToggle();
                $("#expand-form-button").hide(1);
                document.getElementById("mc-embedded-subscribe-form").action ="http://skyflex.us14.list-manage.com/subscribe/post?u=5321ce4878800c447658224d3&id=7fa42966ee";
            }
        });
    });

Also I want to make 3 input field check if these are not empty.

Tomasz Jakub Rup
  • 10,502
  • 7
  • 48
  • 49
eatmailyo
  • 670
  • 1
  • 12
  • 33

5 Answers5

4

try this :

$(document).ready(
    function() {
    $(".form-fieldError").hide();
        $("#expand-form-button").click(function() {
            if( document.getElementById('sign_up_email').value === '' ){
                $(".form-fieldError").show(1);
            }else{
                $("#form-expand").fadeToggle();
                $("#expand-form-button").hide(1);
                document.getElementById("mc-embedded-subscribe-form").action ="http://skyflex.us14.list-manage.com/subscribe/post?u=5321ce4878800c447658224d3&id=7fa42966ee";
            }
        });



$('#form-expand').keypress(function (e) {
  if (e.which == 13) {
    $('#expand-form-button').click();
    return false;  
  }
});


    });
Nitya Kumar
  • 967
  • 8
  • 14
1

I guess something like this is what you are looking for ? If you have any problems with that feel free to ask.

$('form').keypress(function (e) {
  if (e.which == 13) {                   // 13 = keycode for Enter
    $('input').each(function(){          // the 3 (or more) inputs to check
      var value = $(this).val();
      if(!(value.lenth > 0)){            // If Inputs are empty
        alert("You have to fill in the given inputs");
        return false;                    // return and don't go on
      }
    });
    $('.yourform').submit();             // submit form
    return false;
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Aaron Mahlke
  • 337
  • 1
  • 8
  • Can you add that validation in my code above this post? Im new in JavaScript and I want to see, how it looks and learn something from that :) – eatmailyo Oct 05 '16 at 07:10
1

I am not shure if that is exactly what you want.. but I think that should be a good start!

$(document).ready(function() {

function expandFormBtn() {
    $('input').each(function(){
      var value = $(this).val();
      if(!(value.length > 0)){
        $(".form-fieldError").show(1);
        return;
      }
    });
    $("#form-expand").fadeToggle();
    $("#expand-form-button").hide(1);
    $("#mc-embedded-subscribe-form").action ="http://skyflex.us14.list-manage.com/subscribe/post?u=5321ce4878800c447658224d3&amp;id=7fa42966ee";
    return false;
  
}

  $(".form-fieldError").hide();
  $("#expand-form-button").click(function() {
   expandFormBtn();
  });
  
  $('form').keypress(function (e) {
    if (e.which == 13) {
      expandFormBtn();
    }
  });
});
Aaron Mahlke
  • 337
  • 1
  • 8
0

for enter key You need event and then you can check event=13 that is ascii for enter key

$("#entersomething").keyup(function(e) {
    alert("up");
    var code = (e.keyCode ? e.keyCode : e.which);
    if (code==13) {
       //do something
    }


});

or you can do in keypress which I found more reliable

$(document).keypress(function(event) {
    var keycode = event.keyCode || event.which;
    if(keycode == '13') {
        alert('You pressed a "enter" key in somewhere');    
    }
});
Tanmay
  • 590
  • 8
  • 20
0

I got something, Im trigger button click on Enter key press, thats the easiest way.

    $(document).ready(function() {
    $(".form-fieldError").hide();
        $("#expand-form-button").click(function() {
            if( document.getElementById('sign_up_email').value === '' ){
                $(".form-fieldError").show(1);
            }else{
                $("#form-expand").fadeToggle();
                $("#expand-form-button").hide(1);
                document.getElementById("mc-embedded-subscribe-form").action ="http://skyflex.us14.list-manage.com/subscribe/post?u=5321ce4878800c447658224d3&amp;id=7fa42966ee";
            }
        });

     $('#sign_up_email').keypress(function(e){
        if(e.which == 13){//Enter key pressed
            $('#expand-form-button').click();//Trigger search button click event

        }
    });
});

Now I want to add the field validation if empty (or mistake on email), then show div ,where is error

Tomasz Jakub Rup
  • 10,502
  • 7
  • 48
  • 49
eatmailyo
  • 670
  • 1
  • 12
  • 33