If Value Equals None Don't Call Function

3 posts Page 1 of 1
Contributors
User avatar
mikethedj4
VIP - Site Partner
VIP - Site Partner
Posts: 2592
Joined: Thu Mar 25, 2010 4:36 am

Someone recently asked me this question so I thought I'd write a simple tutorial on it.
There's a variety of reasons as to why this is necessary.

First we need to call and if else statement.
We need to say that if our value is "equal to" empty we tell the user why this is. Usually the case is there's no data to add. Will just put down "function denied". However if it does have a value we can use our else statement to apply whatever function we need.

Here's how it's done using JQuery. (See it in action here)
Code: Select all
$(document).ready(function() {
  $(".call").on('click', function() {
   if ( $(".code").val() === "" ) {
      alert("Function denied!");
    } else {
      alert("Function accepted!");
    }
  });
});
and here is how it's done in standalone Javascript.
Code: Select all
document.addEventListener('DOMContentLoaded', function() {
  var code = document.querySelector('.code'),
    call = document.querySelector('.call');

  call.addEventListener('click', function() {
    if ( code.value === "" ) {
      alert("Function denied!");
    } else {
      alert("Function accepted!");
    }
  });
});
Now other times you may need to use this type of functionality but you can't use an if else statement. This is where conditional (aka ternary) operators become a relevant use.
Code: Select all
<condition> ? <true-value> : <false-value>
Here's an example of how a conditional operator is used.
Code: Select all
"+ $(".add-css-selector-val").val() +" {"+ ( $(".grab-pos-top").val() === "" ? "" : "{ top:" + value + ";}\n" ) + "
Last edited by mikethedj4 on Thu Apr 03, 2014 8:58 pm, edited 1 time in total.
User avatar
Usman55
VIP - Site Partner
VIP - Site Partner
Posts: 2821
Joined: Thu Dec 24, 2009 7:52 pm

Off-topic question but is Javascript easy to learn? I'm thinking of getting into it or JAVA after my exams.
Image
User avatar
mikethedj4
VIP - Site Partner
VIP - Site Partner
Posts: 2592
Joined: Thu Mar 25, 2010 4:36 am

Usman55 wrote:
Off-topic question but is Javascript easy to learn? I'm thinking of getting into it or JAVA after my exams.
JQuery is far easier to learn over traditional Javascript, and it's motto "write less do more" is pretty accurate. JQuery is a very nice library especially for those that are just getting started. If you try to learn native Javascript before JQuery you're probably gonna get a headache and hate Javascript. These libraries save an exponential amount of time coding, and also makes cross browser compatibility much easier.

However like any new language it does take a learning curve. I would suggest JQuery over traditional Javascript. There's a huge community out there and you can almost always find help on your code/project. I've been doing this for over 6 years. My first language was HTML/CSS/Javascript, and then VB.NET which I haven't used in a long time.

I have tons of tutorials posted on here, my YouTube, and live demos you can view and edit on Codepen. All of my apps are open source. So finding help is never a problem, but solving it takes a bit of understanding, and problem solving. You do that well in VB.NET so I don't think learning JQuery would be hard for you.
3 posts Page 1 of 1
Return to “Tutorials”