Reputation: 95
I have a dynamic table which allows a person to upload an image, their name and surname within a modal when they click a button. When I try upload the image my table displays "undefined" in the place of the image. How do I get the table to display the image?
I've tried using .val(), .attr('src'). I am new to jquery so I'm sure I'm making a rookie mistake here, but I'm out of ideas and can't seem to find a similar issue anywhere.
$(document).ready(function () {
$("#save_btn").click(function () {
let image = $("#Image").attr('');
let name = $("#Name").val();
let surname = $("#Surname").val();
$('#myTable').append(
'<tr>' +
'<td>' + image + '</td>' +
'<td>' + name + '</td>' +
'<td>' + surname + '</td>' +
'<td> <button id="edit" class="btn btn-sm material-icons" style="color: green"> create </button> </td>' +
'<td><button id="delete" class="btn btn-sm material-icons" style="color: red">delete</button></td>' +
'</tr>'
);
});
});
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
<title>Homework 2</title>
</head>
<body>
<div class="container">
<div class="row">
<div class="col">
<button type="button" class="btn btn-primary btn-lg btn-block" data-toggle="modal" data-target="#myModal">Add to Table</button>
</div>
</div>
<table id="myTable" class="table table-dark">
<thead>
<tr>
<th scope="col">Image</th>
<th scope="col">Name</th>
<th scope="col">Surname</th>
<th scope="col">Edit</th>
<th scope="col">Delete</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
<!-- The Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Add Data to Table</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form>
<div class="form-group">
<label id="Image_input" for="Image" class="col-form-label">Image:</label>
<input type="file" class="form-control" id="Image">
</div>
<div class="form-group">
<label id="name_input" for="Name" class="col-form-label">Name:</label>
<input type="text" class="form-control" id="Name">
</div>
<div class="form-group">
<label id="surname_input" for="Surname" class="col-form-label">Surname:</label>
<input type="text" class="form-control" id="Surname">
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button id="save_btn" type="button" class="btn btn-primary" data-dismiss="modal">Save</button>
</div>
</div>
</div>
</div>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
<script src="js/index.js"></script>
</body>
</html>
I made a change to the append function. I added this...
"<td>" + "<img id='table_image' alt='' src='" + './images/' + image + "' > " + "</td>" +
Upvotes: 1
Views: 1982
Reputation: 1580
$(document).ready(function () {
$('input[type="file"]').change(function(e){
fileName=URL.createObjectURL(e.target.files[0]);
});
$("#save_btn").click(function () {
//let image = $("#Image").attr('src',fileName);
let name = $("#Name").val();
let surname = $("#Surname").val();
$('#myTable').append(
'<tr>' +
'<td><img src="' + fileName + '" width="150" height="150"></td>' +
'<td>' + name + '</td>' +
'<td>' + surname + '</td>' +
'<td> <button id="edit" class="btn btn-sm material-icons" style="color: green"> create </button> </td>' +
'<td><button id="delete" class="btn btn-sm material-icons" style="color: red">delete</button></td>' +
'</tr>'
);
});
});
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
<title>Homework 2</title>
</head>
<body>
<div class="container">
<div class="row">
<div class="col">
<button type="button" class="btn btn-primary btn-lg btn-block" data-toggle="modal" data-target="#myModal">Add to Table</button>
</div>
</div>
<table id="myTable" class="table table-dark">
<thead>
<tr>
<th scope="col">Image</th>
<th scope="col">Name</th>
<th scope="col">Surname</th>
<th scope="col">Edit</th>
<th scope="col">Delete</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
<!-- The Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Add Data to Table</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form>
<div class="form-group">
<label id="Image_input" for="Image" class="col-form-label">Image:</label>
<input type="file" class="form-control" id="Image">
</div>
<div class="form-group">
<label id="name_input" for="Name" class="col-form-label">Name:</label>
<input type="text" class="form-control" id="Name">
</div>
<div class="form-group">
<label id="surname_input" for="Surname" class="col-form-label">Surname:</label>
<input type="text" class="form-control" id="Surname">
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button id="save_btn" type="button" class="btn btn-primary" data-dismiss="modal">Save</button>
</div>
</div>
</div>
</div>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
<script src="js/index.js"></script>
</body>
</html>
Upvotes: 1
Reputation: 553
You are assigning blank attr value. Please see updated code as below.
$(document).ready(function() {
$("#save_btn").click(function() {
var fileNameIndex = $("#Image").val().lastIndexOf("\\") + 1;
var image = $("#Image").val().substr(fileNameIndex);
let name = $("#Name").val();
let surname = $("#Surname").val();
$('#myTable').append(
'<tr>' +
'<td>' + image + '</td>' +
'<td>' + name + '</td>' +
'<td>' + surname + '</td>' +
'<td> <button id="edit" class="btn btn-sm material-icons" style="color: green"> create </button> </td>' +
'<td><button id="delete" class="btn btn-sm material-icons" style="color: red">delete</button></td>' +
'</tr>'
);
});
});
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
<title>Homework 2</title>
</head>
<body>
<div class="container">
<div class="row">
<div class="col">
<button type="button" class="btn btn-primary btn-lg btn-block" data-toggle="modal" data-target="#myModal">Add to Table</button>
</div>
</div>
<table id="myTable" class="table table-dark">
<thead>
<tr>
<th scope="col">Image</th>
<th scope="col">Name</th>
<th scope="col">Surname</th>
<th scope="col">Edit</th>
<th scope="col">Delete</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
<!-- The Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Add Data to Table</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form>
<div class="form-group">
<label id="Image_input" for="Image" class="col-form-label">Image:</label>
<input type="file" class="form-control" id="Image">
</div>
<div class="form-group">
<label id="name_input" for="Name" class="col-form-label">Name:</label>
<input type="text" class="form-control" id="Name">
</div>
<div class="form-group">
<label id="surname_input" for="Surname" class="col-form-label">Surname:</label>
<input type="text" class="form-control" id="Surname">
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button id="save_btn" type="button" class="btn btn-primary" data-dismiss="modal">Save</button>
</div>
</div>
</div>
</div>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
<script src="js/index.js"></script>
</body>
</html>
Upvotes: 0