Remove Filelist object [duplicate] - javascript

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.

Related

Delete element in associative array [duplicate]

This question already has answers here:
How do I unset an element in an array in javascript?
(8 answers)
Closed 5 years ago.
I'm trying to create a button so that when clicked, a specific element stored in an associative array will get deleted. But what I've done doesn't seem to work. Any help will be appreciated.
// Creates the button
bodyText = bodyText + '<input type="button" id="btnDeleteQuestion"
value="Delete a question">';
document.getElementById("btnDeleteQuestion")
.addEventListener("click", function() {
// Deletes the third question stored in the questionBank
delete questionBank[2]['questionText'];
console.log(questionBank);
});
}
Use splice (if it is an array) to remove the element from the array.
questionBank.splice(2, 1); // from the third element, remove 1 item.
What you have written in your question will get the questionBank[2] and then delete a property called questionText in the object.
It will also throw an error if the array does not have at least 3 items or if the 3rd item is null or undefined
I hope you helpful this
var questionBank= ["questionText", "questionText1", "questionText2"];
document.getElementById("btnDeleteQuestion").addEventListener("click", function() {
// index get by text
const index = questionBank.indexOf("questionText");
if(index!=-1){
//Delete element by index
questionBank.splice(index, 1);
}
console.log(questionBank);
});

Loop through Drop Down Option and read its attribute using jquery [duplicate]

This question already has answers here:
How to get the attributes of a HTML element using JQuery?
(4 answers)
Closed 8 years ago.
I am looping through drop down option and checking attribute.
if attribute match than counter is increase. At the end i show counter as alert.
This is my code but some how its not working dont know why
var count= 0;
$('.mydropdown option').each(function () {
var level = this.attr("myattr");
if (level == "0") {
count++;
}
});
alert(count);
}
this is a plain javascript object, it does not contain a function called .attr()
Try,
var level = $(this).attr("myattr");
Just convert the this reference into a jquery object and invoke .attr() over it

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

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/

values of html_options with javascript [duplicate]

This question already has answers here:
jQuery get values of checked checkboxes into array
(9 answers)
Closed 8 years ago.
Using javascript, how can I get the array of values selected in checkboxes used within a smarty html_options. Ive tried $('#selectbox').val() which returns null.
$("#trigger").click(function () {
var selected = $("input:checked");
var values = [];
selected.each(function (i,d) {
values.push(d.value);
});
console.log(values);
});
http://jsfiddle.net/698Ec/ this is a demo

How to remove item from a JavaScript object [duplicate]

This question already has answers here:
How do I remove a property from a JavaScript object?
(37 answers)
Closed 3 years ago.
How can I remove an item from a JavaScript object?
Like this:
var test = {'red':'#FF0000', 'blue':'#0000FF'};
test.remove('blue');
var test = {'red':'#FF0000', 'blue':'#0000FF'};
delete test.blue; // or use => delete test['blue'];
console.log(test);
this deletes test.blue

Categories

Resources