0

I've a script and his code snippet:

but.onclick = function() {
    var form;
    var constructor = new Constructor();
    var builder = new BidFormBuilder();
    constructor.setFormBuilder(builder);
    constructor.constructForm();
    constructor.displayForm();
    form = constructor.getForm();
    form.getSubmitButton().onclick = function cl() {
        var phone = form.getPhone().childNodes[1].childNodes[1];
        if (phone.value === '') {
            //Re-run code instruction must been here.
        }
    };
};

How to re-run the entire script when phone.value === '' is true?

2
  • Put the code inside a function, like function buildForm() { }, and in the if statement, if it passes, call buildForm() function Commented Feb 24, 2014 at 11:48
  • @RaraituL I'm trying to do as you said, but after I'm click SubmitButton buildForm() function doesn't invoked. I've no errors in console. Commented Feb 24, 2014 at 11:50

3 Answers 3

1

You can try to use something like this if you want to re-run your event again,in this case you will get a recursion.(So you should fix it in condition)

but.onclick = function() {
    execute();
};


function execute(){
    var form;
    var constructor = new Constructor();
    var builder = new BidFormBuilder();
    constructor.setFormBuilder(builder);
    constructor.constructForm();
    constructor.displayForm();
    form = constructor.getForm();
    form.getSubmitButton().onclick = function cl() {
        var phone = form.getPhone().childNodes[1].childNodes[1];
        if (phone.value === '') {
            //Re-run code instruction must been here.
            execute();
        }
    };
}
Sign up to request clarification or add additional context in comments.

Comments

0

Ever heard of named functions?

but.onclick = yourfunctionhere; 

function yourfunctionhere() {
   var form;
   var constructor = new Constructor();
   var builder = new BidFormBuilder();
   constructor.setFormBuilder(builder);
   constructor.constructForm();
   constructor.displayForm();
   form = constructor.getForm();
   form.getSubmitButton().onclick = function cl() {
      var phone = form.getPhone().childNodes[1].childNodes[1];
      if (phone.value === '') {
         yourfunctionhere();
      }
   };    
}

Comments

0

Try it like this.

but.onclick = clickHandler;

function clickHandler() {
    var form;
    var constructor = new Constructor();
    var builder = new BidFormBuilder();
    constructor.setFormBuilder(builder);
    constructor.constructForm();
    constructor.displayForm();
    form = constructor.getForm();
    form.getSubmitButton().onclick = function cl() {
        var phone = form.getPhone().childNodes[1].childNodes[1];
        if (phone.value === '') {
            clickHandler(); /* Call the function again */
        }
    };
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.