0

I have this modal from my blade file

                    <div id="addBtn" class="modal fade in" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                        <div class="modal-dialog">
                            <div class="modal-content">
                                <div class="modal-header">
                                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                                    <h5 class="modal-title" id="myModalLabel">Add a medicine</h5>
                                </div>
                                <div class="modal-body">
                                    <form>
                                     <div class="form-group">
                                        <label class="control-label mb-10">Generic Name</label>
                                        <select class="form-control" name="medicine_id">
                                            @foreach($items as $item)
                                            <option value="{{$item->id}}" >{{$item->generic_name}}</option>
                                            @endforeach
                                        </select>
                                    </div>

                                    <div class="form-group">
                                        <label class="control-label mb-10">Dosage Volume</label>
                                        <input type="text" name="dosage_name" class="form-control" placeholder="Example: 20mg">
                                    </div>

                                    <div class="form-group">
                                        <label class="control-label mb-10">Form</label>
                                        <input type="text" name="form" class="form-control" placeholder="Bottle, Tablet">
                                    </div>

                                    <div class="form-group">
                                        <label class="control-label mb-10">Price Per piece</label>
                                        <input type="text" name="price"  class="form-control" placeholder="Price">
                                    </div>

                                    <div class="form-group">
                                        <label class="control-label mb-10">Insert a photo</label>
                                        <div class="panel-wrapper collapse in">
                                            <div class="panel-body">
                                               <div class="mt-20">
                                                <input type="file" name="photo" id="input-file-now" class="dropify" >
                                            </div>  
                                        </div>
                                    </div>
                                </div>

                            </form>
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-success waves-effect" data-dismiss="modal" id="save-dosage">Save</button>
                            <button type="button" class="btn btn-default waves-effect" data-dismiss="modal">Cancel</button>
                        </div>

                    </div>
                    <!-- /.modal-content -->
                </div>
            </div>

and once the id of save-dosage button is clicked

this should be called

function addDosage(url){

    $('#save-dosage').click(function(e){
        console.log(e);

    });


}

Now the thing here is that I want to call the function of addDosage that is in an external file called crud.js

I'm using laravel by the way so this is my end script codes

<script>
    var token = '{{ Session::token() }}';
    var url = '{{ route('storeDosage') }}';
</script>
<script type="text/javascript" src="{{ URL::to('assets/scripts/crud.js') }}"></script>
@endsection
4
  • 1
    There's nothing stopping you from doing that, if addDosage is global. Commented Feb 17, 2017 at 11:25
  • What prevents you from doing this? Is there something that's actually not working? Commented Feb 17, 2017 at 11:26
  • This is my script files: <script type="text/javascript" src="{{ URL::to('assets/dashboard/dist/js/init.js') }}"></script> <script> var token = '{{ Session::token() }}'; var url = '{{ route('storeDosage') }}'; </script> <script type="text/javascript" src="{{ URL::to('assets/scripts/crud.js') }}"></script> Commented Feb 17, 2017 at 11:30
  • do I have to add onclick="addDosage()" Commented Feb 17, 2017 at 11:34

3 Answers 3

1

If your crud.js file is properly included. Then No need to call it in the DOM file because click is event that will not fires on the DOM ready as if you call it in the DOM file.

So Write in your crud.js like below

 $('#save-dosage').click(function(e){

     var url = "" ;  
     addDosage(url)

  });

  function addDosage(url){

      console.log("Here");

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

Comments

0

You can call it. When you add the scripts to your HTML page using <script> tag, they become a part of the page dynamically(became global). So you can call and it will execute.

Comments

0

If the problem is in access to other file, the easy way is to include it like this: <script src="crud.js"></script>.

Then in crud.js add listener like this:

$(function(){
  $('#save-dosage').click(function(e){
      console.log(e);
  });
});

6 Comments

<script> var token = '{{ Session::token() }}'; var url = '{{ route('storeDosage') }}'; </script> <script type="text/javascript" src="{{ URL::to('assets/scripts/crud.js') }}"></script>
Oh, I see now. Try this instead: $(function{$('#save-dosage').click(function(e){ console.log(e); });}); It means "when jQuery is loaded add onClick listener to #save-dosage."
Ok, give me something a bit
It says unexptected token
Kindly improve your answers
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.