enter your url
click
function display()
{
var myimage=document.createElement("img"); myimage.src=document.getElementById("image"); document.body.appendChild(myimage); }
B/htm
This is how you can show the image on the same window
See the code below:
let submitForm = document.getElementById('submit')
submitForm.addEventListener('click', function(){
let imageUrl = document.getElementById('getUrl').value;
if (imageUrl) {
document.getElementById('uploadedImage').setAttribute('src', imageUrl);
document.getElementById('uploadedImage').style.display = 'block';
}
});
<label>Enter Image Url:</label>
<input type='url' id='getUrl'>
<input type='submit' id='submit'/>
<img id="uploadedImage" src="#" alt="Uploaded Image" accept="image/png, image/jpeg" style="display:none;">
I'm working on a webapp locally and I have a 'settings' page. The user can change the variables and the Javascript is supposed to save the values but it seems to reset them back to 1 straight after it changes them.
HTML:
<body onload="getSettings()">
<h1>Instance Load Manager - Settings</h1>
<div id="uploader">
<h2>Terraform file upload:</h2>
<input type="file" id="file" name="files[]" multiple onchange="moveFile()" />
</div>
<a href="index.html">
<div id="settingsButton">
<h3>Back</h3>
</div>
</a>
<div id ="settingsForm">
<form>
Maximum instances:<br>
<input type="text" id="maxInst" name="max instances"><br>
<br>
<input type="submit" value="Save Changes" onclick="updateSettings()">
</div>
</body>
Javascript:
var maxInstances = 1;
function updateSettings()
{
maxInstances = document.getElementById("maxInst").value;
alert(maxInstances);
}
function getSettings()
{
document.getElementById("maxInst").value = maxInstances;
}
Help appreciated :)
You need to prevent your page from refreshing after submitting the form.
function updateSettings(e)
{
e.preventDefault();
maxInstances = document.getElementById("maxInst").value;
alert(maxInstances);
}
I'm dynamically building a tabbed display of files based on user input from the file chooser that is
displayed for input type=file
<input type="file" id="selectedFiles" style="display: none;" multiple/>
<input type="image" src="file_select_button_image.png" onClick="buildFileTabs();"/>
The problem is that when the user clicks on the file select button the file chooser is displayed ANDbuildFileTabs is called(). This is happening with the chromium browser. I have not tried any other browsers.
How to I associate the buildFileTabs() function with the closing of the file chooser dialog?
Use the onchange event on the <input type="file" />.
function showFilePicker() {
document.getElementById("selectedFiles").click();
}
function buildFileTabs(e) {
var msg = "Selected "+ this.files.length+" files";
var div = document.body.appendChild(document.createElement("div"));
div.innerHTML = msg;
}
<input type="file" id="selectedFiles" style="display: none;" multiple onchange="buildFileTabs.call(this, event)" />
<input type="button" value="file_select_button_image.png" onclick="showFilePicker();"/>
Using the given form, I first click on "Choose File", open the file,its name comes next to it
and then I click on "Upload" button" to upload the image.What I want to do is click on "Choose File", open the file but the name should not appear next to it and the image should get uploaded without clicking the "Upload button" after this.How can this be achieved?
<div id="image_container" name="image_container">
<img src="../image/ab.jpg" alt="Cover" width="900px" height="500px">
<div class="btn-group" id="cov" name="cov" >
<button class="btn dropdown-toggle" data-toggle="dropdown" id="mybtn" onclick="dropdown()">Action</button>
<ul class="dropdown-menu" id="dropdown-menu" style="display:none">
<!-- dropdown menu links -->
<li><form action="upload.php" method="post" enctype="multipart/form-data" id="MyUploadForm">
<input name="ImageFile" id="imageInput" type="file" />
<label id="fileup" for="imageInput">Upload File</label>
<input type="submit" id="submit-btn" value="Upload" />
<img src="img/loader.gif" id="loading-img" style="display:none;" alt="Please Wait"/>
</form>
<div id="output"></div>
<li>Something else here</li>
<li class="divider"></li>
<li>Another link</li>
</ul>
</div>
</div>
$(document).ready(function() {
var options = {
target: '#output', // target element(s) to be updated with server response
beforeSubmit: beforeSubmit, // pre-submit callback
success: afterSuccess, // post-submit callback
resetForm: true // reset the form after successful submit
};
$('#MyUploadForm').submit(function() {
$(this).ajaxSubmit(options);
// always return false to prevent standard browser submit and page navigation
return false;
});
});
Check this Demo Fiddle.
If you are using jQuery,
$('#imageInput').change(function(){
$("#submit-btn").click(); //$('#MyUploadForm').submit();
});
Or Just,
<input name="ImageFile" id="imageInput" type="file" onChange="this.submit();" />
To hide the filename, you have to manipulate the css, or hide the whole button and place another button in it.
document.getElementById("imageInput").onchange = function() {
document.getElementById("MyUploadForm").submit();
};
What about that?
<input name="ImageFile" id="imageInput" type="file"
onChange="this.submit();this.visibility='hidden';" />
You can use a label to create your own upload button and just hide the input itself.
HTML
<form action="#">
<label for="upload">Pretend Button</lable>
<input type="file" id="upload" name="image">
</form>
JS
$('#upload').on('change', function () {
$(this).closest('form').trigger('submit');
});
CSS
input {
position: absolute;
top: 0;
left: -9999px;;
}
Here is a small demo: http://jsbin.com/rirupeco/2/edit?html,css,js,output
Maybe a lil user experience trick and also inspiration for your request:
When files selected and clicking OK in file-selection-window, it should upload right after. No extra click on Submit, by this:
var flag_fileSelection = false;
$("#element").click(function(){
flag_fileSelection = true;
})
$(window).focus(function(){
if (flag_fileSelection) {
flag_fileSelection = false;
// user is done choosing files
$("#form").submit();
}
})
I am creating a form which allows people to enter a video url. Standard input form, to accept a URL like:
http://video.google.com/videoplay?docid=1299927595688205543
I would like to add a button (within the form, which says something like [preview video]). Basically that button/link appends the link they entered into the input field, to this code:
<a href="http://video.google.com/videoplay?docid=1299927595688205543&lightbox[width]=610&lightbox[height]=360" class="lightbox">google video</a >
This is to be available prior to form submission.
<form id="video_upload_form" action="">
<label for="video_input_box">Video URL</label>
<input type="text" id="video_input_box" value="" />
<input type="submit" value="Add Video" />
</form>
<p>Preview Video</p>
<script type="text/javascript">
$().ready(function(){
$('#video_upload_form').submit(function(){
var video_url_params = '&lightbox[width]=610&lightbox[height]=360';
var video_url = $('#video_input_box').val() + video_url_params;
$('#video_preview').attr('href', video_url);
return false;
});
});
</script>
Also here it is as a JSFiddle: http://jsfiddle.net/Treffynnon/CEMpT/
You will need to do some validation on the URL supplied by the user before putting it into the preview link, but I will leave that part up to you as you have not asked for help with it in your question.