Editing the filename in a HTML file input element [duplicate] - javascript

This question already has answers here:
Dynamically set value of a file input [duplicate]
(4 answers)
Closed 8 years ago.
I am trying to prevent IE from sending the full file path to the server on a file upload. So I am trying to parse out the filename. In my HTML I have:
<input id="fileupload" type="file" name="cutsheet" data-url="ws/cutsheet/representation">
So if the value attribute is "c:\folder\file.txt", I want to change that to "file.txt". I have the following javascript:
$('#fileupload').change(function() {
var index = this.value.lastIndexOf('\\');
this.value = this.value.substring(index + 1);
alert(this.value);
});
However, my alert shows "c:\folder\file.txt". Can I not set the value attribute's value?

Try split()
$('#fileupload').change(function(e) {
var input = $(this).val();
var pieces = input.split('\\');
alert(pieces[pieces.length-1]);
});
Fiddle : http://jsfiddle.net/V5Qd8/

Related

Datatype .innerHTML [duplicate]

This question already has answers here:
How do I get the value of text input field using JavaScript?
(16 answers)
Closed 2 years ago.
I´m getting a NaN out of this script, is it a data type problem and how do i solve it?
<script>
function calc(){
var profile = document.getElementById("profil");
var hours = document.getElementById("hour");
document.getElementById("result").innerHTML=profile*hours;
}
</script>
Present the result like this
<button onclick="calc()">Calc</button><br>
<p id="result"></p>
You have to get the value from the element.
var profile = document.getElementById("profil").value;
With .getElementById you get a DOM Element.
Supposing "profil" and "hour" are input elements, you want to obtain their values:
<script>
function calc(){
var profile = document.getElementById("profil").value;
var hours = document.getElementById("hour").value;
document.getElementById("result").innerHTML=profile*hours;
}
</script>

Remove Filelist object [duplicate]

This question already has answers here:
How to remove one specific selected file from input file control
(5 answers)
Closed 3 years ago.
I was trying to delete or remove an object from Filelist of <input type="file"multiple/> in Javascript or JQuery but could not remove or delete the object.
I was trying to delete with an operator like delete $(input[type="file"]).files[0]; but is not working either.
You can do something like this:
let deleteButton = document.getElementById("deleteFiles");
deleteButton.onclick = function () {
let element = document.getElementById("filesInput");
console.log("Files: ");
console.log(element.files);
element.value = '';
console.log("Files after removal: ");
console.log(element.files);
}
<input type="file" id="filesInput">
<button id="deleteFiles">delete file</button>
This is implemented on vanillaJavascript but you can use Jquery to help you throw the implementation.

how to select a value from drop down using jquery that got generated from txt file [duplicate]

This question already has answers here:
Access Java / Servlet / JSP / JSTL / EL variables in JavaScript
(5 answers)
Closed 5 years ago.
I am generating a drop down from a text file using "load" function in jquery. I am in need to select a dynamic value from this drop down based on my session variable.
$("#id").val(session variable) is not working.
Is there any other way to select value from this drop down?
you can use a scriptlet to transfer the session variable to javascript like so:
var sessVar = '<%= session.getAttribute("something") %>';
Then you can do the following to get the value:
var id = "#" + sessVar;
var dropValue = $(id).val();
and to set a value you do:
$("#id").val(sessVar);
lets suppose your Select dropdown id is : "dropdownlist1"
Use :
var SelectedValue = $('#dropdownlist1').val();
To Assign value using jquery:
$('#dropdownlist1').val("SomeText");

How to retrieve setAttribute from servlet into JavaScript [duplicate]

This question already has answers here:
Access Java / Servlet / JSP / JSTL / EL variables in JavaScript
(5 answers)
Closed 6 years ago.
I'm trying to retrieve the value of my setAttribute into my javascript code but I get a syntax error when I run the page.
Here is the code of my setAttribute in my servlet:
request.setAttribute("codeIGA", codeIGA); //a string is being passed here
Here is the code of my JS:
$(document).ready(
function () {
var count = 0;
var lastCodeIGA = ${sessionScope.codeIGA}; // syntax error here
console.log(lastCodeIGA);
.
.
.
etc
How can I retrieve the value from my servlet and input it into my JS?
You can use following to retrieve the required value.
var lastCodeIGA = '${codeIGA}';
Try with scriptlet.
var test = <%=request.getAttribute("codeIGA");
Or
var lastCodeIGA = '${codeIGA}';

how to view the answer of a js function in a html input? [duplicate]

This question already has answers here:
Set the value of an input field
(17 answers)
Closed 7 years ago.
I've always been wondering this, but all websites seem to be answering the opposite (how to make text typed in an input be the input of a function).
Is there a way to do it?
Is this what you want to do?
function myFunction() {
// some functionality here
// this function must return something
}
var input = document.querySelector('input');
input.value = myFunction();
This will set the value of the input element to the value returned by myFunction.

Categories

Resources