change form action is not working - form not defined

change form action is not working - form not defined

I have a form that allows users to edit the administrators of a post but also create new administrators for the post.

So this form has some radio buttons. Each radio button corresponds to an administrator of a post. When some radio button (administrator) is clicked the details of the administrator are populated in the form fields. When the "Update" button has clicked the details of the administrator are updated.

But then there is a static radio button, "Create new administrator", that allows creating a new administrator. When this radio button is selected the form fields are reset so the user can insert values to create a new admin when it clicks on the "Store" button.

This first part was working fine.


But then I needed to add some jQuery to change the form action based on radio button selection, so its possible to update but also create administrators when the "Create new admin" radio button is selected.

 But when adding this code "form.attr('action', '{{route('admins.store', ['post_id' => $post->id])}}');" and "form.attr('action', '{{route('admins.update', ['post_id' => $post->id])}}');"  that first part above stops working. 

And in the console it appears the error "form is not defined" on this line "form.attr('action', '{{route('admins.store', ['post_id' => $post->id])}}');" .

Do you know why?


jQuery:

  1. var admins = {!!  $administrators !!}

  2.     $("input[name='radiobutton']").click(function() {

  3.     if($(this).attr("id") == "create_administrator"){
  4.         $("#adminUpdateButton").hide();
  5.         $("#adminStoreButton").show();
  6.         form.attr('action', '{{route('admins.store', ['post_id' => $post->id])}}');
  7.     }
  8.     else{
  9.         $("#adminUpdateButton").show();
  10.         $("#adminStoreButton").hide();
  11.         form.attr('action', '{{route('admins.update', ['post_id' => $post->id])}}');
  12.     }

  13.     let id = $(this).attr("id");

  14.     let data = administrators.find(e => e.id == id) || {
  15.         name: "",
  16.        email: "",
  17.        ...: ""
  18.     };
  19.    $("input[name='name']").val(data.name);
  20.     ...
  21.     });

Form:

  1. <form method="post" class="clearfix" action="{{route('admins.update', ['id' => $post->id])}}" enctype="multipart/form-data">
  2.     {{csrf_field()}}
  3.     @foreach($administrators $admin)
  4.           <div class="form-check">
  5.             <input class="form-check-input" type="radio" name="radiobutton" id="{{$admin->id}}" value="">
  6.             <label class="form-check-label" for="">
  7.               {{$admin->name}}
  8.             </label>
  9.           </div>
  10.     @endforeach
  11.     <div class="form-check">
  12.       <input class="form-check-input" type="radio" name="radiobutton" id="create_administrator"
  13.              value="option2">
  14.       <label class="form-check-label" for="exampleRadios2">
  15.           Create new administrator
  16.       </label>
  17.   </div>
  18.   <div class="form-group">
  19.     <label for="name">Name</label>
  20.     <input type="text" required class="form-control" value="{{ $admin->name }}" name="name">
  21.   </div>

  22.   <!-- below I have more form fields like administrator name, email, etc -->

  23.   <input type="submit" id="adminStoreButton" class="btn btn-primary" value="Create"/>
  24.   <input type="submit" id="adminUpdateButton" class="btn btn-primary" value="Update"/>
  25.   </form>