Display input title only "onclick" event - javascript

I'm having trouble in displaying my title input only "onclick" event.
When we mouseouver the input box, it will display the title, but i just want this to happen when I click on the input box.
My idea is displaying a title saying "value copied", cause I have a function on "onclick" event that copy the input (read only) value to the clipboard, and when it's done I want to let the user know that this happen displaying that information.
Is that even possible?
(this is for a windows gadget)
Thank you to everyone.

Try .focus()
<!DOCTYPE html>
<html>
<head>
<style>span {display:none;}</style>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
</head>
<body>
<p><input type="text" /> <span>focus fire</span></p>
<p><input type="password" /> <span>focus fire</span></p>
<script>
$("input").focus(function () {
$(this).next("span").css('display','inline').fadeOut(1000);
});
</script>
</body>
</html>
http://api.jquery.com/focus/

Related

Javascript function not firing with html onclick

I am battling to get my javascript function to fire when a button is clicked on my html page.
My html code snippet:
<!DOCTYPE html>
<html>
<head>
<title>VLA SYSTEM</title>
Logout
<script
type ="text/JavaScript" src="logout.js"></script>
</head>
<body>
<form>
<script
type ="text/JavaScript" src="regGen.js"></script>
<script
type="text/JavaScript" src="submit.js"></script>
<input type="button" name="btnAssign" value="Assign Owner To Registration Number..." onclick="submit()">
My javascript code snippet:
function submit() {
window.alert("Information Captured And Stored To Database!");
}
The idea is that, when the user clicks on the button, the alert should fire informing the user that information has been captured, however, I am unable to get this alert to appear.
Any advice would be greatly appreciated.
The problem is that, unintuitively, inline handlers essentially implicitly use with (this) for the element that triggered the event, and for the parent element, and so on:
<form>
<input type="button" name="btnAssign" value="Assign Owner To Registration Number..."
onclick="console.log(submit === document.querySelector('form').submit)"
>
</form>
And form.submit is a built-in function will submit the form.
Either use a different function name, like doAlert:
onclick="doAlert()"
and
function doAlert() {
window.alert("Information Captured And Stored To Database!");
}
Or attach the listener properly using Javascript instead (inline handlers are pretty bad practice, and can result in unintuitive behavior like you're seeing):
document.querySelector('input[name="btnAssign"]).addEventListener('click', submit);

javascript onChange from input clicked by another element [duplicate]

This question already has answers here:
How can I prevent refresh of page when button inside form is clicked?
(16 answers)
Closed 4 years ago.
there have been similar topics posted however none I found solved my problem.
Here's a sample html page with some javascript
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<script>
window.onload = function() {
var fileDlg = document.getElementById('fileDialog');
// this should detect onChange, but not working
fileDlg.onchange = (e) => {
window.alert('Changed: '+e.target.value);//
//document.getElementById('frm_img_upload').submit();
}// eo filedlg.onchange
}//eo window onload
</script>
</head>
<body>
<form id="frm_img_upload" name="frm_img_upload" method="post" enctype="multipart/form-data">
<!-- hidden ugly as hell file input element. Note onChange doesn't happen -->
<input type="file" accept="image/*" name="fileDialog" id="fileDialog" style="opacity:0;" onChange="javascript:alert('Changed');">
<!-- when this little button is clicked, the file dialog is triggered, however theres no onChange -->
<button onClick="document.getElementById('fileDialog').click();" >Pick New Image</button>
</form>
</body>
</html>
The idea was to hide the super-annoying input file type element, and create a button with an onclick, which will click the invisible input. This works fine and dandy, however, once the visitor has selected a file and the dialog closes, I want to submit the form, rather than have another annoying button to press.
The onchange is not firing.
I'm not using jquery for this, just good ol javascript.
Any ideas?
Thanks.
Your button is submitting the form as soon as the dialog closes, since the default is type="submit", so the onchange doesn't get a chance to run.
Change it to type="button" and then you'll see your alert.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<script>
window.onload = function() {
var fileDlg = document.getElementById('fileDialog');
// this should detect onChange, but not working
fileDlg.onchange = (e) => {
window.alert('Changed: '+e.target.value);//
//document.getElementById('frm_img_upload').submit();
}// eo filedlg.onchange
}//eo window onload
</script>
</head>
<body>
<form id="frm_img_upload" name="frm_img_upload" method="post" enctype="multipart/form-data">
<!-- hidden ugly as hell file input element. Note onChange doesn't happen -->
<input type="file" accept="image/*" name="fileDialog" id="fileDialog" style="opacity:0;" onChange="javascript:alert('Changed');">
<!-- when this little button is clicked, the file dialog is triggered, however theres no onChange -->
<button type="button" onClick="document.getElementById('fileDialog').click();" >Pick New Image</button>
</form>
</body>
</html>

onchange event of text box not working if i change text value dynamically

Onchange Event Fired when i change on textbox but when i am trying to change textbox value dynamically it doesnot fired why?
here is my simple demo when i click on button i changed textbox value dynamically but still textbox onchange event doesnot fired.
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
</head>
<body>
<h1>My First Heading</h1>
<p>My first paragraph.</p>
<input type="text" id="txtno" value="" onchange="javascript:alert('Hi');">
<input type="button" id="btnadd" value="ClickMe">
<script>
$(document).ready(function(){
$("#btnadd").click(function(){
$("#txtno").val('Hello');
});
});
</script>
</body>
</html>
Changing value of input via JS will not trigger .onChange events. You must trigger it manually
$('#room_1').trigger('change');
I don't see where you change input field value in your code.
The 'change' event is only fired on controls when the user commits a value change. If you make changes to a field programatically then you need to trigger the 'change' event.
In this case:
<script>
$(document).ready(function(){
$("#btnadd").click(function(){
$("#txtno").val('Hello').trigger('change');
});
});
</script>

Adding JavaScript events to an HTML input

This code isn't working in Firefox but working on IE and Chrome. When I click inside the text box it runs the focusOut() and focusIn() functions. Is this behavior expected and is the syntax recommended?
<!doctype html>
<html>
<head>
<title>Page of Cage</title>
<meta charset="UTF-8">
<link rel="icon" href="mkx-logo.png" type="image/x-icon">
<script type="text/javascript">
function focusIn() {
alert("focus In!");
};
function focusOut() {
alert("focus OUT!");
};
</script>
</head>
<body>
<form>
Name:<input onfocus="focusIn()" onblur ="focusOut()" type="text" name="name"/> <br/>
<input type="submit" value ="submit"/>
</form>
</body>
</html>
Replace alert with console.log and watch your web browser console using the developer tools. Just like nnnnnn said in the comments.
Q:Why the code is not working?
A: onfocus is fired when focus is gained and onblur is fired when focus is lost. In your code when you focus on the textbox it generates onfocus and at the same time the alert box is generated and it has lost the focus. So, Both the function are launched.
Check out this Fiddle

How to modify the value of text input with js/jquery?

I would like to dynamically modify the values of certain form elements, more specifically certain input text fields. So far, whenever I load my html page, I am just getting the blank input field, but I am expecting it to contain the value of 1. Here is an example of how I am trying to do this.
<!DOCTYPE HTML>
<HTML>
<HEAD>
<script type="text/javascript" src="javascript/jquery-1.6.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var myForm = $(this).getElementById('form1');
myForm.elements['Q01'].value = '1';
});
</script>
</HEAD>
<BODY>
<form id="form1">
<input type="text" name="Q01" maxlength="1" />
</form>
</BODY>
</HTML>
The reason this needs to be done dynamically is because the value of form could be different every time. Am I even approaching this correctly? Suggestions on how I can achieve my intended functionality?
-- EDIT --
None of the solutions seem to be doing the trick. Here is an update of my code:
<!DOCTYPE HTML>
<HTML>
<HEAD>
<script type="text/javascript" src="javascript/jquery-1.6.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
//$("#Q01").val("1");
$("#form1 input[name='Q01']").val("1");
//$("input[name='Q01']").val('1');
});
</script>
</HEAD>
<BODY>
<form id="form1">
<input type="text" id="Q01" name="Q01" maxlength="1" />
</form>
</BODY>
</HTML>
I am expecting when I load the page, that the input text will have 1 in it. But the input text keeps showing up empty. Any ideas?
-- EDIT --
Here is a solution derived from the answers from below that I like:
<!DOCTYPE HTML>
<HTML>
<HEAD>
<script type="text/javascript" src="javascript/jquery-1.6.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#Q01").val("1");
});
</script>
</HEAD>
<BODY>
<form id="form1">
<input type="text" id="Q01" name="Q01" maxlength="1" />
</form>
</BODY>
</HTML>
If you are using jQuery you could just change the id attribute to name in the input elements and then do something like this:
$('#Q01').val('1')
The val method sets the value sou can find more here: http://api.jquery.com/val/
You've got your ready handler correct. One of the great things about jQuery is its selector engine. I recommend taking a look at the documentation to help familiarize yourself with it.
To do what you want, I'd recommend something like this:
$(document).ready(function() {
$("#form1 input[name='Q01']").val("1");
});
The #form1 is the same as document.getElementById("form1"). The input[name='Q01'] grabs all input elements that have a name attribute equal to Q01. The .val method sets the selected elements value to the string 1.
$(document).ready(function(){
$("form input[name='Q01']).val('1');
)};
This should work:
$("input[name='Q01']").val('1');
But why not to set id's to your inputs?
Hope it works

Categories