JavaScript Calling an anonymous function within if of another function calling anonymous function matlab,calling anonymous function javascript,calling anonymous

I'm trying to "insert" an alerter if the username of a basic form is not filled and I have been helplessly trying to understand why the anonymous function within a submit function doesn't get called.

JIC on top of my html I have a prepared block for the alert:

<div id="alerter">
</div>

The function in question:

<script>
  function validateForm() {
    var x = document.forms["formz"]["username"].value;
    if (x == "") {
      (function() {
        alert("whoa");
        var div = document.createElement('div');
        div.setAttribute('class', 'alert alert-primary alert-dismissible fade show');
        div.setAttribute('role', 'alert');
        div.innerHTML = document.getElementById('alertonempty').innerHTML;
        document.getElementById('alerter').appendChild(div);
      })
      return false;
    }
  }
</script>

<script id="alertonempty" type="text/html">
  <strong>SMTH went wrong</strong> Seriously wrong
  <button type="button" class="close" data-dismiss="alert" aria-label="Close">
    <span aria-hidden="true">&times;</span>
  </button>
</script>

And yes, there is a form that calls validateForm. It does take the username variable and can use it (checked it also with an alert) however the anonymous function doesn't get called.

Please help me understand

Answer:1

What you've created is a auto-executing anonymous function, and in order to make that function work properly, you need to add a second pair of parenthesis at the end :

function validateForm() {
    var x = document.forms["formz"]["username"].value;
    if (x == "") {
      (function() {
        alert("whoa");
        var div = document.createElement('div');
        div.setAttribute('class', 'alert alert-primary alert-dismissible fade show');
        div.setAttribute('role', 'alert')
        div.innerHTML = document.getElementById('alertonempty').innerHTML;
        document.getElementById('alerter').appendChild(div);
      })();
//-----^^^^
      return false;
    }
  }
Answer:2

I have a table and using ng-repeat data is populated, which is obtained from JSON Table has name, phone and time when i click on name a bootstrap modal pops up, but i need to have selected name ...

I have a table and using ng-repeat data is populated, which is obtained from JSON Table has name, phone and time when i click on name a bootstrap modal pops up, but i need to have selected name ...

  1. bootstrap modal should not close on escape
  2. bootstrap modal should not close on click
  3. bootstrap modal should not close when click outside

When is asObservable() needed on a Subject (e.g. BehaviorSubject) to get a observable of the subject? The subject isself can be casted to an Observable as well. Questions What are the technical ...

When is asObservable() needed on a Subject (e.g. BehaviorSubject) to get a observable of the subject? The subject isself can be casted to an Observable as well. Questions What are the technical ...

I wrote a gulp file that works but...not at any attempts. Indeed, I get this weird error 1 time / 3: [12:25:51] Starting 'default'... [12:25:51] Starting 'clean'... [12:25:51] Finished 'clean' ...

I wrote a gulp file that works but...not at any attempts. Indeed, I get this weird error 1 time / 3: [12:25:51] Starting 'default'... [12:25:51] Starting 'clean'... [12:25:51] Finished 'clean' ...

  1. gulp unhandled stream error in pipe
  2. gulp uglify unhandled stream error in pipe

I need to scroll to a form field (a displayfield actually) automatically. Since the field is dynamic, sometimes it is already shown in the rendered area of page, sometimes it is not. How can I do ...

I need to scroll to a form field (a displayfield actually) automatically. Since the field is dynamic, sometimes it is already shown in the rendered area of page, sometimes it is not. How can I do ...