How can I associate a button with a form using Angular?












0















I'm working with Angular 6.x, and I'd like to associate a submit button located outside the form in the DOm with it. That is, I want to accomplish something structurally equivalent to this:



<button type='submit' form='myform'>
click me!
</button>

<form id='myform' action='#' onsubmit='console.log("wheee")'>
<input type='submit' value='me too'/>
</form>


I.e. I want to handle the submit event from the form element.



Is there a way to do this without going through nativeElement or without moving/duplicating the submit handler on the click event of the button?










share|improve this question



























    0















    I'm working with Angular 6.x, and I'd like to associate a submit button located outside the form in the DOm with it. That is, I want to accomplish something structurally equivalent to this:



    <button type='submit' form='myform'>
    click me!
    </button>

    <form id='myform' action='#' onsubmit='console.log("wheee")'>
    <input type='submit' value='me too'/>
    </form>


    I.e. I want to handle the submit event from the form element.



    Is there a way to do this without going through nativeElement or without moving/duplicating the submit handler on the click event of the button?










    share|improve this question

























      0












      0








      0








      I'm working with Angular 6.x, and I'd like to associate a submit button located outside the form in the DOm with it. That is, I want to accomplish something structurally equivalent to this:



      <button type='submit' form='myform'>
      click me!
      </button>

      <form id='myform' action='#' onsubmit='console.log("wheee")'>
      <input type='submit' value='me too'/>
      </form>


      I.e. I want to handle the submit event from the form element.



      Is there a way to do this without going through nativeElement or without moving/duplicating the submit handler on the click event of the button?










      share|improve this question














      I'm working with Angular 6.x, and I'd like to associate a submit button located outside the form in the DOm with it. That is, I want to accomplish something structurally equivalent to this:



      <button type='submit' form='myform'>
      click me!
      </button>

      <form id='myform' action='#' onsubmit='console.log("wheee")'>
      <input type='submit' value='me too'/>
      </form>


      I.e. I want to handle the submit event from the form element.



      Is there a way to do this without going through nativeElement or without moving/duplicating the submit handler on the click event of the button?







      javascript angular






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 16 '18 at 3:57









      millimoosemillimoose

      32.5k763105




      32.5k763105
























          3 Answers
          3






          active

          oldest

          votes


















          1















          You can achieve by reference #form of form and passing the same reference to button.




          <button type='button' (click)="form.onsubmit()">
          click me!
          </button>

          <form id='myform' #form action='#' onsubmit='console.log("wheee")'>
          <input type='submit' value='me too'/>
          </form>


          Working copy is here - https://stackblitz.com/edit/angular-pnneks






          share|improve this answer































            1














            Tyr it like this.



            <button type='submit'  (click)="myForm.submit()">
            click me!
            </button>

            <form id='myform' action='#' #myForm onsubmit='console.log("wheee")'>
            <input type='submit' value='me too'/>
            </form>





            share|improve this answer































              0














              Well, you could use a Reactive Form and then on the click of this button. call a function that would extract the value of the form and do something on it.



              This way, you can also do things like disabling the button if the form is invalid.



              So here's what your form will look like:



              import { Component } from '@angular/core';
              import { FormGroup, FormBuilder, FormControl, Validators } from '@angular/forms';

              @Component({
              selector: 'my-app',
              templateUrl: './app.component.html',
              styleUrls: [ './app.component.css' ]
              })
              export class AppComponent {
              userForm: FormGroup;

              constructor(private fb: FormBuilder) {}

              ngOnInit() {
              this.userForm = this.fb.group({
              firstName: [, Validators.required],
              lastName: [, Validators.required]
              });
              }

              submitForm() {
              console.log('Form Submitted with value: ', this.userForm.value);
              }
              }


              And here's the template:



              <h3>User Form</h3>
              <hr>
              <form [formGroup]="userForm">
              <div>
              <label for="firstName">First Name: </label>
              <input type="text" id="firstName" formControlName="firstName">
              </div>
              <br>
              <div>
              <label for="lastName">Last Name: </label>
              <input type="text" id="lastName" formControlName="lastName">
              </div>
              </form>
              <br>
              <button (click)="submitForm()" [disabled]="userForm.invalid">Submit</button>


              Here's a Sample StackBlitz for your ref.






              share|improve this answer
























                Your Answer






                StackExchange.ifUsing("editor", function () {
                StackExchange.using("externalEditor", function () {
                StackExchange.using("snippets", function () {
                StackExchange.snippets.init();
                });
                });
                }, "code-snippets");

                StackExchange.ready(function() {
                var channelOptions = {
                tags: "".split(" "),
                id: "1"
                };
                initTagRenderer("".split(" "), "".split(" "), channelOptions);

                StackExchange.using("externalEditor", function() {
                // Have to fire editor after snippets, if snippets enabled
                if (StackExchange.settings.snippets.snippetsEnabled) {
                StackExchange.using("snippets", function() {
                createEditor();
                });
                }
                else {
                createEditor();
                }
                });

                function createEditor() {
                StackExchange.prepareEditor({
                heartbeatType: 'answer',
                autoActivateHeartbeat: false,
                convertImagesToLinks: true,
                noModals: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: 10,
                bindNavPrevention: true,
                postfix: "",
                imageUploader: {
                brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                allowUrls: true
                },
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                });


                }
                });














                draft saved

                draft discarded


















                StackExchange.ready(
                function () {
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53331220%2fhow-can-i-associate-a-button-with-a-form-using-angular%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                3 Answers
                3






                active

                oldest

                votes








                3 Answers
                3






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                1















                You can achieve by reference #form of form and passing the same reference to button.




                <button type='button' (click)="form.onsubmit()">
                click me!
                </button>

                <form id='myform' #form action='#' onsubmit='console.log("wheee")'>
                <input type='submit' value='me too'/>
                </form>


                Working copy is here - https://stackblitz.com/edit/angular-pnneks






                share|improve this answer




























                  1















                  You can achieve by reference #form of form and passing the same reference to button.




                  <button type='button' (click)="form.onsubmit()">
                  click me!
                  </button>

                  <form id='myform' #form action='#' onsubmit='console.log("wheee")'>
                  <input type='submit' value='me too'/>
                  </form>


                  Working copy is here - https://stackblitz.com/edit/angular-pnneks






                  share|improve this answer


























                    1












                    1








                    1








                    You can achieve by reference #form of form and passing the same reference to button.




                    <button type='button' (click)="form.onsubmit()">
                    click me!
                    </button>

                    <form id='myform' #form action='#' onsubmit='console.log("wheee")'>
                    <input type='submit' value='me too'/>
                    </form>


                    Working copy is here - https://stackblitz.com/edit/angular-pnneks






                    share|improve this answer














                    You can achieve by reference #form of form and passing the same reference to button.




                    <button type='button' (click)="form.onsubmit()">
                    click me!
                    </button>

                    <form id='myform' #form action='#' onsubmit='console.log("wheee")'>
                    <input type='submit' value='me too'/>
                    </form>


                    Working copy is here - https://stackblitz.com/edit/angular-pnneks







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 16 '18 at 4:06









                    Sunil SinghSunil Singh

                    6,4472628




                    6,4472628

























                        1














                        Tyr it like this.



                        <button type='submit'  (click)="myForm.submit()">
                        click me!
                        </button>

                        <form id='myform' action='#' #myForm onsubmit='console.log("wheee")'>
                        <input type='submit' value='me too'/>
                        </form>





                        share|improve this answer




























                          1














                          Tyr it like this.



                          <button type='submit'  (click)="myForm.submit()">
                          click me!
                          </button>

                          <form id='myform' action='#' #myForm onsubmit='console.log("wheee")'>
                          <input type='submit' value='me too'/>
                          </form>





                          share|improve this answer


























                            1












                            1








                            1







                            Tyr it like this.



                            <button type='submit'  (click)="myForm.submit()">
                            click me!
                            </button>

                            <form id='myform' action='#' #myForm onsubmit='console.log("wheee")'>
                            <input type='submit' value='me too'/>
                            </form>





                            share|improve this answer













                            Tyr it like this.



                            <button type='submit'  (click)="myForm.submit()">
                            click me!
                            </button>

                            <form id='myform' action='#' #myForm onsubmit='console.log("wheee")'>
                            <input type='submit' value='me too'/>
                            </form>






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 16 '18 at 4:06









                            Gabriel LopezGabriel Lopez

                            28217




                            28217























                                0














                                Well, you could use a Reactive Form and then on the click of this button. call a function that would extract the value of the form and do something on it.



                                This way, you can also do things like disabling the button if the form is invalid.



                                So here's what your form will look like:



                                import { Component } from '@angular/core';
                                import { FormGroup, FormBuilder, FormControl, Validators } from '@angular/forms';

                                @Component({
                                selector: 'my-app',
                                templateUrl: './app.component.html',
                                styleUrls: [ './app.component.css' ]
                                })
                                export class AppComponent {
                                userForm: FormGroup;

                                constructor(private fb: FormBuilder) {}

                                ngOnInit() {
                                this.userForm = this.fb.group({
                                firstName: [, Validators.required],
                                lastName: [, Validators.required]
                                });
                                }

                                submitForm() {
                                console.log('Form Submitted with value: ', this.userForm.value);
                                }
                                }


                                And here's the template:



                                <h3>User Form</h3>
                                <hr>
                                <form [formGroup]="userForm">
                                <div>
                                <label for="firstName">First Name: </label>
                                <input type="text" id="firstName" formControlName="firstName">
                                </div>
                                <br>
                                <div>
                                <label for="lastName">Last Name: </label>
                                <input type="text" id="lastName" formControlName="lastName">
                                </div>
                                </form>
                                <br>
                                <button (click)="submitForm()" [disabled]="userForm.invalid">Submit</button>


                                Here's a Sample StackBlitz for your ref.






                                share|improve this answer




























                                  0














                                  Well, you could use a Reactive Form and then on the click of this button. call a function that would extract the value of the form and do something on it.



                                  This way, you can also do things like disabling the button if the form is invalid.



                                  So here's what your form will look like:



                                  import { Component } from '@angular/core';
                                  import { FormGroup, FormBuilder, FormControl, Validators } from '@angular/forms';

                                  @Component({
                                  selector: 'my-app',
                                  templateUrl: './app.component.html',
                                  styleUrls: [ './app.component.css' ]
                                  })
                                  export class AppComponent {
                                  userForm: FormGroup;

                                  constructor(private fb: FormBuilder) {}

                                  ngOnInit() {
                                  this.userForm = this.fb.group({
                                  firstName: [, Validators.required],
                                  lastName: [, Validators.required]
                                  });
                                  }

                                  submitForm() {
                                  console.log('Form Submitted with value: ', this.userForm.value);
                                  }
                                  }


                                  And here's the template:



                                  <h3>User Form</h3>
                                  <hr>
                                  <form [formGroup]="userForm">
                                  <div>
                                  <label for="firstName">First Name: </label>
                                  <input type="text" id="firstName" formControlName="firstName">
                                  </div>
                                  <br>
                                  <div>
                                  <label for="lastName">Last Name: </label>
                                  <input type="text" id="lastName" formControlName="lastName">
                                  </div>
                                  </form>
                                  <br>
                                  <button (click)="submitForm()" [disabled]="userForm.invalid">Submit</button>


                                  Here's a Sample StackBlitz for your ref.






                                  share|improve this answer


























                                    0












                                    0








                                    0







                                    Well, you could use a Reactive Form and then on the click of this button. call a function that would extract the value of the form and do something on it.



                                    This way, you can also do things like disabling the button if the form is invalid.



                                    So here's what your form will look like:



                                    import { Component } from '@angular/core';
                                    import { FormGroup, FormBuilder, FormControl, Validators } from '@angular/forms';

                                    @Component({
                                    selector: 'my-app',
                                    templateUrl: './app.component.html',
                                    styleUrls: [ './app.component.css' ]
                                    })
                                    export class AppComponent {
                                    userForm: FormGroup;

                                    constructor(private fb: FormBuilder) {}

                                    ngOnInit() {
                                    this.userForm = this.fb.group({
                                    firstName: [, Validators.required],
                                    lastName: [, Validators.required]
                                    });
                                    }

                                    submitForm() {
                                    console.log('Form Submitted with value: ', this.userForm.value);
                                    }
                                    }


                                    And here's the template:



                                    <h3>User Form</h3>
                                    <hr>
                                    <form [formGroup]="userForm">
                                    <div>
                                    <label for="firstName">First Name: </label>
                                    <input type="text" id="firstName" formControlName="firstName">
                                    </div>
                                    <br>
                                    <div>
                                    <label for="lastName">Last Name: </label>
                                    <input type="text" id="lastName" formControlName="lastName">
                                    </div>
                                    </form>
                                    <br>
                                    <button (click)="submitForm()" [disabled]="userForm.invalid">Submit</button>


                                    Here's a Sample StackBlitz for your ref.






                                    share|improve this answer













                                    Well, you could use a Reactive Form and then on the click of this button. call a function that would extract the value of the form and do something on it.



                                    This way, you can also do things like disabling the button if the form is invalid.



                                    So here's what your form will look like:



                                    import { Component } from '@angular/core';
                                    import { FormGroup, FormBuilder, FormControl, Validators } from '@angular/forms';

                                    @Component({
                                    selector: 'my-app',
                                    templateUrl: './app.component.html',
                                    styleUrls: [ './app.component.css' ]
                                    })
                                    export class AppComponent {
                                    userForm: FormGroup;

                                    constructor(private fb: FormBuilder) {}

                                    ngOnInit() {
                                    this.userForm = this.fb.group({
                                    firstName: [, Validators.required],
                                    lastName: [, Validators.required]
                                    });
                                    }

                                    submitForm() {
                                    console.log('Form Submitted with value: ', this.userForm.value);
                                    }
                                    }


                                    And here's the template:



                                    <h3>User Form</h3>
                                    <hr>
                                    <form [formGroup]="userForm">
                                    <div>
                                    <label for="firstName">First Name: </label>
                                    <input type="text" id="firstName" formControlName="firstName">
                                    </div>
                                    <br>
                                    <div>
                                    <label for="lastName">Last Name: </label>
                                    <input type="text" id="lastName" formControlName="lastName">
                                    </div>
                                    </form>
                                    <br>
                                    <button (click)="submitForm()" [disabled]="userForm.invalid">Submit</button>


                                    Here's a Sample StackBlitz for your ref.







                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Nov 16 '18 at 4:59









                                    SiddAjmeraSiddAjmera

                                    16k31239




                                    16k31239






























                                        draft saved

                                        draft discarded




















































                                        Thanks for contributing an answer to Stack Overflow!


                                        • Please be sure to answer the question. Provide details and share your research!

                                        But avoid



                                        • Asking for help, clarification, or responding to other answers.

                                        • Making statements based on opinion; back them up with references or personal experience.


                                        To learn more, see our tips on writing great answers.




                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function () {
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53331220%2fhow-can-i-associate-a-button-with-a-form-using-angular%23new-answer', 'question_page');
                                        }
                                        );

                                        Post as a guest















                                        Required, but never shown





















































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown

































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown







                                        Popular posts from this blog

                                        Florida Star v. B. J. F.

                                        Error while running script in elastic search , gateway timeout

                                        Adding quotations to stringified JSON object values