Search code examples
jqueryjsptextareaspring-form

Filling a textarea by input file - jquery


EDIT In short my jsp is like this:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<script src="../js/jquery/jquery.min.js" type="text/javascript" charset="utf-8"></script>
<script src="../js/jquery/jquery-ui.min.js"></script>
<script src="../js/jquery/jquery.validate.min.js" type="text/javascript"></script>
<script src="../js/myJs.js" type="text/javascript"></script>
<script src="../js/loadText.js" type="text/javascript"></script>
<link href="../css/jquery/jquery-ui.css" rel="stylesheet" type="text/css">
<link href="../css/jquery/jquery-ui.theme.min.css" rel="stylesheet" type="text/css">
<link href="../css/jquery/jquery-ui.min.css" rel="stylesheet" type="text/css">
<link href="../css/jquery/jquery-ui.structure.css" rel="stylesheet" type="text/css">
<link href="../css/jquery/jquery-ui.structure.min.css" rel="stylesheet" type="text/css">
<link href="../css/jquery/jquery-ui.theme.css" rel="stylesheet" type="text/css">
<link href="../css/form/form.css" rel="stylesheet" type="text/css">
<title>Add</title>
</head>
<body>
<form:form  modelAttribute="add" method="POST" name="add" action="${pageContext.request.contextPath}/add/myForm" id="myForm">
  <fieldset>
    <legend>Insert new:</legend>
      <div id="id" class="element">
        <label>ID:</label><br/>
            <form:input path="id"/>
      </div>
      <div id="title" class="element">
        <label>*Title:</label><br/>
            <form:input path="title" required="required"/>
      </div>
      <div id="shortDescr" class="element">
        <label>*Short Description:</label><br/>
            <form:textarea path="shortDescr" rows="4" cols="50" required="required"/>
      </div>
      <div id="description" class="element">
        <label>Description:</label>
        <input type="file" value="Upload" id="file" accept=".txt"/><input type='button' id='load-file' value='Load'><br/>
            <form:textarea path="description" rows="4" cols="50" placeholder="description"/>
      </div>
      <br/>
      <br/>
      <br/>
      <br/>
        <input type="submit" name="submit" value="Immetti">
        </fieldset>
    </form:form>
</body>
</html>

Other .js don't touch the description. The form send data to controller, that wrap the result inside an object. I validate my form with jquery validate like so: Redirect in post mode inside a controller's method


Searching how I could change textarea value uploading a file I found this cool solution: Get data from file input in JQuery and change the code for my needs like so: https://jsfiddle.net/2madvyqb/7/

<input type="file" id="file" accept=".txt"/>
<input type='button' id='load-file' value='Load'>
<div id="file-content"></div>
<textarea id="description" name="description" rows="4" cols="50">
description</textarea>

and in the js file I substitute the result with

$('textarea#description').text(fr.result);

It works fine in jsfiddle, but it's not working in my project. I don't see any error in the browser console, and it seems to me that the others jquery*.js are in the right order.

$('textarea#description').val(fr.result);

don't work either.

I'm using the spring-form.


Solution

  • I found an answer here: https://stackoverflow.com/a/23116463/4477899 Just put the script before the </body> tag