-1

But when date is not 15, then it calls both methods. How can i make sure that if 1st method returns false then other method should not be called.

Below is the code,

<input type="text" id="txtDate" name="SelectedDate" onchange="javascript:myMethod1(); javascript:myMethod2();" />

<script>
function myMethod1(){
            var dateVal = new Date(document.getElementById("txtDate").value);               
            var dateday = dateVal.getDate();

            if(dateday != 15){                  
                document.getElementById("mySpan1").innerHTML = "Error Message";
                return false

            }else{                  
                return true;            
            }
        }

        function myMethod2(){               
            document.getElementById("mySpan2").innerHTML = "No Error";          
        }
  </script>
RohitT
  • 187
  • 2
  • 11

4 Answers4

4

Nobody mentioned the most obvious and most used solution. You can use the short-circuit AND (&&) operator.

The short-circuit AND operator evaluate the right side only if the left side evaluated as true (or truthy). If on the right side there's a function call, that function will be called only if the left side evaluated as true:

(true && f()) // f will be called
(false && f()) // f will NOT be called

You can substitute true and false literals with other function calls.

In your specific case, you just need to substitute the semicolon with a double and:

onchange="javascript:(myMethod1() && myMethod2())"  

function f1() {
  console.log('f1 called');
  return Math.random() < 0.5;
}

function f2() {
  console.log('f2 called');
}
<button onclick="javascript:(f1() && f2())">Click me</button>
  • Thanks for the answer @Cristian. It worked like a charm. Just one issue is there. First time when the date is not 15, then it works. Then when i select other date then the Span2 is populated. then when i select date as 15 again, then both the messages can be seen. How can we resolve this ? – RohitT Jun 16 '20 at 11:08
  • @RohitT the logic of your function could be wrong. This is another type of question unrelated to the solution that I provided, I advise you to use a debugger to understand where the problem is and, after that, if you still have problems then ask a new question – Christian Vincenzo Traina Jun 16 '20 at 11:10
  • Was able to resolve the issue by making the other spans blank in the if conditions of both the methods. - @Cristian – RohitT Jun 16 '20 at 11:23
0

How about this???

var shouldExecuteNext = function1();

if(!shouldExecuteNext){
function2();
}
Kunal Vohra
  • 2,703
  • 2
  • 15
  • 33
0

why are you calling both function at the same time if you dont want to execute them simultaneously. if you want to execute the 2nd method only when the 1st one returns true then try to call the 2nd method inside the 1st method in else block where you return true like this:

<input type="text" id="txtDate" name="SelectedDate" onchange="javascript:myMethod1();" />

simply add the line in script below the following line like:

console.log("mySpan2");
myMethod();

try this.

0

Ideally you'd want to create a single function that calls myMethod1 and/or myMethod2 based on some condition:

<input
  type="text"
  id="txtDate"
  name="SelectedDate"
  onchange="handleInputChange()"
/>

<span id="mySpan1"></span>
<span id="mySpan2"></span>

<script>
function handleInputChange() {
  var dateVal = new Date(document.getElementById("txtDate").value);
  var dateday = dateVal.getDate();
  
  if (dateday !== 15) {
    myMethod1()
  } else {
    myMethod2()
  }
}

function myMethod1() {
  document.getElementById("mySpan1").innerHTML = "Error Message";
}

function myMethod2() {
  document.getElementById("mySpan2").innerHTML = "No Error";
}
</script>
goto
  • 4,336
  • 15
  • 20