I've been trying to implement a modal on my site that dynamically loads the content using an ajax call.
I stumbled across this question, which showed this demo, which would suit my needs perfectly after a little modification.
The demo works perfectly on JSFiddle, but inside my dev environment is spitting out the error:
Uncaught TypeError: Cannot set property 'innerHTML' of undefined
Tracing it back to my script it is saying that htmlData is undefined, but it is defined right above it?
Script that runs the call:
<script>
(function() {
var infoModal = $('#myModal');
$('.modal-toggle').on('click', function(){
$.ajax({
type: "GET",
url: '/api/menu-item/'+$(this).data('id'),
dataType: 'json',
error: function(data){
fakeResponse = {"id":4,"menu_category_id":446,"name":"kunzereichert","description":"Dolores impedit ut doloribus et a et aut.","price":"999.99","created_at":"2015-04-10 05:55:23","updated_at":"2015-04-10 05:55:23"}
;
var htmlData = '<ul><li>';
htmlData += fakeResponse.name;
htmlData += '</li></ul>';
infoModal.find('#modal-body')[0].innerHTML = htmlData;
}
});
});
})(jQuery);
</script>
I'm not very fluent with javascript, so would appreciate any help with why I am getting this error. I am just trying to test loading some information from a JSON response to then display inside the modal.
If there is a better way to do this then I am open to suggestions to!
Edit: this is the HTML code for my page
@extends('sbadmin')
@section('content')
<div class="col-lg-10 col-lg-offset-1">
<h1><i class="fa fa-users"></i> Staff</h1>
<div class="table-responsive">
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>Name</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach ($staff as $employee)
<tr>
<td>{{ $employee->first_name }} {{ $employee->last_name }}</td>
<td><button type="button" class="modal-toggle" data-toggle="modal" data-target="#myModal" data-id="{{ $employee->id }}">Disable</button><a href="{{ url('staff/regularhours/' . $employee->id) }}" class="btn btn-primary">Staff Details</a> <a href="{{ url('staff/regularhours/' . $employee->id) }}" class="btn btn-success">Regular Hours</a></td>
</tr>
@endforeach
</tbody>
</table>
</div>
{!! $staff->render() !!}
</div>
<!-- Modal -->
<div id="myModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Modal Header</h4>
</div>
<div class="modal-body">
<p></p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<script>
(function() {
var infoModal = $('#myModal');
$('.modal-toggle').on('click', function(){
$.ajax({
type: "GET",
url: '/api/menu-item/'+$(this).data('id'),
dataType: 'json',
error: function(data){
fakeResponse = {"id":4,"menu_category_id":446,"name":"kunzereichert","description":"Dolores impedit ut doloribus et a et aut.","price":"999.99","created_at":"2015-04-10 05:55:23","updated_at":"2015-04-10 05:55:23"}
;
var htmlData = '<ul><li>';
htmlData += fakeResponse.name;
htmlData += '</li></ul>';
infoModal.find('#modal-body')[0].innerHTML = htmlData;
}
});
});
})(jQuery);
</script>
@endsection