Daniels Jirgensons Daniels Jirgensons - 1 year ago 71
Javascript Question

Check for Button press and Enter press

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.

Answer Source

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&id=7fa42966ee";
    return false;
  
}

  $(".form-fieldError").hide();
  $("#expand-form-button").click(function() {
  	expandFormBtn();
  });
  
  $('form').keypress(function (e) {
    if (e.which == 13) {
      expandFormBtn();
    }
  });
});

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download