1

Acoording to this post (and many others), I wrote this function:

function cssDisplay(block,none){
  block.css('display','block');
  none.css('display','none');
}

Then, when calling it, I need just one parameter (block or none), like this:

  email.keyup(function(){
      if(email.hasClass('has-error')) {
        cssDisplay(errorEmail,undefined);
      }else{
        button.prop("disabled", false);
        cssDisplay(undefined,errorEmail);
      }
  });

But it only works for the display block. When the input gets valid, the error message won't go away, so I'm missing something. Can someone help me out?

Mellville
  • 1,027
  • 2
  • 18
  • 39
  • It's because you're passing undefined as a parameter, so javascript can't read the undefined property from something that does not exist – moon Dec 01 '17 at 18:43

1 Answers1

1

If you need to change one element then you won't need a function, just change it directly:

email.keyup(function(){
    if(email.hasClass('has-error')) {
        errorEmail.style.display = "block";   // show it
    } else {
        button.prop("disabled", false);
        errorEmail.style.display = "none";    // hide it
    }
});
ibrahim mahrir
  • 31,174
  • 5
  • 48
  • 73
  • well, thanks for the answer, but that was the way i was doing, but since the script is kind of long and the code gets somewhat repeated, I thought having a function...but it's valid as for my question goes – Mellville Dec 01 '17 at 18:47
  • @Mellville You're welcome! You could change `cssDisplay` so that it checks if the parameters are `undefined` or not before accessing their `style`. Or create other helper functions, maybe a combo of `hide(element)`/`show(element)` or a `toggle(element)` that toggles the element visibility. – ibrahim mahrir Dec 01 '17 at 19:02