If Value Equals None Don't Call Function
3 posts
Page 1 of 1
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)
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
and here is how it's done in standalone Javascript.
$(document).ready(function() {
$(".call").on('click', function() {
if ( $(".code").val() === "" ) {
alert("Function denied!");
} else {
alert("Function accepted!");
}
});
});
Code: Select all
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.
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!");
}
});
});
Code: Select all
Here's an example of how a conditional operator is used.
<condition> ? <true-value> : <false-value>
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.
Off-topic question but is Javascript easy to learn? I'm thinking of getting into it or JAVA after my exams.
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
Copyright Information
Copyright © Codenstuff.com 2020 - 2023