0

My Problem

I wanted to use JQuery/Ajax to delete something from my database. But I could not get the function to fire on click of a button. Now I tried something easier, like hide a paragraph on click of a button but it does not work. I used different JQuery methods, but to no avail.

I also tried including JQuery in my master layout like so:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

Here is some of my code:

 <!-- edit.blade.php-->

  <!-- HTML -->

  <button type="button" id="btnhide" class="btn">Hide</button>

  <p>Paragraph 1</p>

  <!-- JavaScript and JQuery -->
  <script>
    $(document).ready(function(e){
      $("btnhide").click(function(){
        $("p").hide();
        });
    });
  </script>

And here the whole blade file:

@extends('adminlte::page')

@section('title', '| Edit Employee')

@section('content_header')
@stop

@section('content')
  <div class='col-lg-4 col-lg-offset-4'>

      <h1><i class='fa fa-user-plus'></i> Edit {{$employee->name}}</h1>
      <hr>

      {{ Form::model($employee, ['route' => ['employees.update', $employee->id], 'method' => 'PUT']) }}

      <div class="form-group">
          {{ Form::label('name', 'Name') }}
          {{ Form::text('name', null, array('class' => 'form-control', 'required' => 'required')) }}
      </div>

      <div class="form-group">
          {{ Form::label('email', 'Email') }}
          {{ Form::email('email', null, array('class' => 'form-control', 'required' => 'required')) }}
      </div>

      <h5><b>Give Role</b></h5>

      <div class='form-group'>
          @foreach ($roles as $role)
              {{ Form::checkbox('roles[]',  $role->id, $employee->roles ) }}
              {{ Form::label($role->name, ucfirst($role->name)) }}<br>
          @endforeach
      </div>

      <div class="form-group">
          {{ Form::label('password', 'Password') }}<br>
          {{ Form::password('password', array('class' => 'form-control', 'placeholder' => ' • • • • • • • • • •', 'required' => 'required')) }}
      </div>

      <div class="form-group">
          {{ Form::label('password', 'Confirm Password') }}<br>
          {{ Form::password('password_confirmation', array('class' => 'form-control', 'required' => 'required')) }}
      </div>

      <div class="form-group">
          {{ Form::label('qualifications', 'Qualifications') }}<br>
          {{ Form::select('qualifications', $employee_qualifications, null, ['size' => 5, 'class' => 'form-control', 'id' => 'selectedqualification']) }}
          <button
             type="button"
             class="btn btn-default pull-right"
             data-toggle="modal"
             data-target="#qualificationModal"
             data-qualifications="{{ $qualifications }}"
             data-qualification_names="{{ $qualification_names }}">
             Add
          </button>
          <button type="button" id="removequali" class="btn btn-danger pull-right">Remove</button>
          <button type="button" id="btnhide" class="btn">Hide</button>
          <br>
          <p> Test 1 </p>
          <br>
      </div>

      {{ Form::submit('Save', array('class' => 'btn btn-primary')) }}
      {{ Form::close() }}

      @include('dispo.employees.add_qualification')
  </div>
@stop

@section('script')
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <!-- JavaScript and JQuery -->
  <script>
    $(document).ready(function(e){
      $("btnhide").click(function(){
        $("p").hide();
        });
    });
  </script>

  <script>
    //Detaches the Qualification from the Employee via Ajax without refreshing the site
    $(document).ready(function(){
      $(".removequali").click(function(e){
        let qualificationid = $("#selectedqualification").val();
        $.ajax({
                   type: 'DELETE',
                   url: "{{URL::route('remove_qualification')}}",
                   dataType: 'json',
                   headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') },
                   data: {
                     'id': qualificationid,
                     'employeeid': "{{$employee->id}}",
                     '_token': "{{ csrf_token() }}"
                   },

                   success: function (data) {
                        alert('success');
                   },
                   error: function (data) {
                        alert(data);
                   }
        });
      });
    });
  </script>

@endsection
6
  • Can you tell me from View Page Souce or Inspect Element, what happens when you click on the link of this script? Is it loading? Commented Aug 27, 2018 at 15:31
  • Hey, how can I see if the script is working when I click on the button? It's only supposed to hide a paragraph Commented Aug 27, 2018 at 16:41
  • Just visit the page, right click and inspect element or view page source. You will find this <script src="{{ asset('node_modules/jquery/dist/jquery.min.js') }}"></script>. Click on it and tell me what are you seeing is it empty or not? Commented Aug 27, 2018 at 17:15
  • Ah, okay, that script. It's not in my file anymore, I included a CDN instead. But it still won't work. Commented Aug 27, 2018 at 17:23
  • Please check your console. Are there any errors? Commented Aug 27, 2018 at 17:45

4 Answers 4

1

The Best thing you can do is use CDN to include jquery in your blade

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

Or download jquery and put it inside public/js folder and now you can include jquery in your balde template by using asset() helper function

asset('js/jquery.min.js')

Observe that there is no public because asset() helper by default loads files from public folder

Sign up to request clarification or add additional context in comments.

7 Comments

I included the CDN in my master layout and in the blade file I'm working on, but I still can not get it to work.
No, I click on the button and nothing happens, no console errors.
can u please post the code so that it can be clear.!
I just updated my question, but what else should I post? I could post the whole blade file, if that would help...
I updated the question to include the whole blade file, could you have another look?
|
0

Check your Chrome console for errors, it should tell you what's wrong, few tips:

  • Try enclosing your jquery calls to after the document is ready:

$(document).ready(function(){
     $(".btn-class").click(function(){
         $("p").hide();
     });
});

4 Comments

I enclosed the call and included a CDN in my master file as well as the blade file I'm currently working on, but that did not help. How can I use the Chrome console to find out what's wrong?
I updated the question to include the whole blade file, could you have another look?
Check your first script $("btnhide") doesn't exist, it should read $("#btnhide") missing the id selector:
Oh gosh, how embarrassing... Thank you, it works now!
0

Node modules directory is not part of the assets You should use the relative or the absolute path

Or using a cdn if you can

4 Comments

Okay, thanks, but is JQuery not already included in Laravel 5.6 by default via the bootstrap.js file?
Well... Did you ran npm install ?
Yes, I did. Just did it again, but no change in behavior.
I updated the question to include the whole blade file, could you have another look?
0
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<script>
$(document).ready(function(){
  $("#btn").click(function(){
    $("p").hide();
    });
});
</script>

Try this make sure you put the

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> before your </head>

11 Comments

I included the CDN in my master layout and in the blade file I'm working on, just to be sure, then I tried your code, but it still won't work...
@Dunrar do you have btn id? Make sure you have one.
I updated also my code try it and make sure your <input type="button" id="btn" value="Hide Paragraph/>
Yes, I have. The button html is this: <button type="button" id="btnhide" class="btn">Hide</button>. I changed your code to $("#btnhide").click(function(){...
@Dunrar then try this step if will work. Put my first line of src before your </head> then put my second script tag after your button. update my code to #btn to #btnhide
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.