String input automatically entered into readonly field - javascript

Is it possible to copy an input from a previously entered field into another uneditable field javascript? Say for example you have two name fields, the first field you must enter your name, once entered the name will automatically be copied in the second text field which is uneditable. I have done a bit of searching around but i can't seem to find anything of use for this specific situation and i'm quite knew to javascript so any sort of help or a nudge in the right direction would be great.

Just use the keyup listener of your input:
<input type="text" id="input1" />
<input type="text" id="input2" readonly='readonly' />
var $input2 = document.getElementById('input2');
var $input1 = document.getElementById('input1');
$input1.addEventListener('keyup', function()
{
$input2.value = $input1.value;
});
here's example: fiddle

Yes you could, just do the follow:
document.getElementById('non_editable_name').value = document.getElementById('editable_name').value;
if you use jquery, you can do:
$('#non_editable_name').val($('#editable_name').val());

Here is a fiddle. You can do this by using jQuery and code:
$(function () {
$('#nameInput').change(function () {
$('#secondInput').val($(this).val());
});
});
with:
<input id="nameInput" type="text"/><br />
<input id="secondInput" type="text" readonly/>

HTML
<input type="text" id="entry" />
<input type="hidden" id="storage" />
JS
(function() {
$('#entry').on('change', function() { $('#storage').val(this.value); });
})();

Like this:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=windows-1250">
<meta name="generator" content="PSPad editor, www.pspad.com">
<title></title>
<script>
function copianome(nome){
document.getElementById('ds_name_result').value = nome;
}
</script>
</head>
<body>
<input type="text" id="ds_name" onkeyup="copianome(this.value)" /><br />
<input type="text" id="ds_name_result" readonly="readonly" />
</body>
</html>

Related

Auto Fill Text Boxes With Main Text Box Content

I'm trying to auto-fill text boxes 1 to 3 with the main text box content so that whatever is typed into the text box "Title" will also appear in the text boxes
Input1, Input2 and Input3. Here is what I have, but I get an error.
<html>
<head>
<script type="text/javascript">
function CopyData(val){
var a = document.getElementById(val.id).value
document.getElementById("CopyText").value=Title
}
</script>
</head>
<body>
Title:<input type="text" name ="Title" id="Text" onkeyup="CopyData(this)"/><br /> <br />
Input 1:<input type="text" name ="Input1" id="CopyText"/><br />
Input 2:<input type="text" name ="Input2" id="CopyText"/><br />
Input 3:<input type="text" name ="Input3" id="CopyText"/><br />
</body>
</html>
try this out:
<html>
<head>
<script type="text/javascript">
function CopyData(val){
var a = document.getElementById(val.id).value
var inputs = document.querySelectorAll(".input");
for(var i=0;i < inputs.length;i++)
{
inputs[i].value = a;
}
}
</script>
</head>
<body>
Title:<input type="text" name ="Title" id="Text" onkeyup="CopyData(this)"/><br /> <br />
Input 1:<input type="text" class="input" name ="Input1" /><br />
Input 2:<input type="text" class="input" name ="Input2" /><br />
Input 3:<input type="text" class="input" name ="Input3" /><br />
</body>
</html>
remarks:
do not use same id for multiple elements. try class instead
you use 'Title' which is not defined, use "a", that was where you stored the input's value
to get many elements at once with simple js, good method is to use "querySelectorAll" with the proper selector.
good luck.
I guess you cannot assign the same id tag to more than one TextBox, so you must end up with a more "hard-coded" javascript function. Moreover, I would use Title.value and not only Title
function CopyData(){
document.getElementById("CopyText1").value=Title.value;
document.getElementById("CopyText2").value=Title.value;
document.getElementById("CopyText3").value=Title.value;
}

how to show an error message beside field in html using javascript please help me [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
i wrote a program using javascript for showing error message beside field instead of alerting but it is not working please help me to work the program
<html>
<head>
<link rel="stylesheet" type="text/css" href="mystyle.css">
<script type="text/javascript">
function check()
{
if(document.getElementById('firstname').value==NULL || myform.firstname.value.length==0)
{
document.getElementById('errorname').value="this is an invalid name";
}
}
</script>
</head>
<body>
<form name="myform">
<p>name</p>
<input type="text" name="firstname" onblur="check()"/>
<span id="errorname"></span>
<br/><input type="button" value="submit" />
</form>
</body>
</html>
you have not given the id to you textbox also pass the value of textbox into the function
like this
<input type="text" name="firstname" id="firstname" onblur="check(this.value)"/>
And JS Function
function check(value)
{
if(value.trim()=="")
{
document.getElementById('errorname').innerHTML="this is an invalid name";
}
}
SEE FIDDLE DEMO
You are not provided ID for your input field. Add an ID and access your input field.
function check(){
if(document.getElementById('firstname').value==""){
document.getElementById('errorname').innerHTML ="this is an invalid name";
}
}
And change your html field like this.
<input type="text" name="firstname" id="firstname" onblur="check()"/>
HTML:
<form id="form">
<p>name</p>
<input type="text" id="firstname" name="firstname" /> <span id="errorname"></span>
<br/>
<input type="submit" value="Submit" />
</form>
JavaScript:
var form = document.getElementById('form'),
firstName = document.getElementById('firstname'),
errorMessage = document.getElementById('errorname');
function check(event) {
event.preventDefault();
if (firstName.value === '' || !firstName.value.length) {
console.log('here')
errorMessage.innerText = 'This is an invalid name';
} else {
errorMessage.innerText = '';
}
}
form.addEventListener('submit', check);
JSFiddle example:
http://jsfiddle.net/2etd93jL/2/
You have used document.getElementById('firstname') but your input input type="text" name="firstname" doesn't have an id, only a name. try adding id="firstname" to the input box.
Multiple errors in your code listing down below:
Use of NULL: it is null and not NULL.
No id assigned as firstname and checking for that in the if loop
to give value to span need to use innerHTML
So your HTML code:
<form name="myform">
<p>name</p><input id="firstname" type="text" name="firstname" onblur="check()"/><span id="errorname"> </span>
<br/><input type="button" value="submit" />
</form>
JS Code:
function check()
{
if(document.getElementById('firstname').value==null || myform.firstname.value.length==0)
{
document.getElementById('errorname').innerHTML="this is an invalid name";
}
}
Working Fiddle
This is almost your code, few changes did to meet the requirement.
Checkout the fiddle
function check(){
if(document.getElementById('firstname').value=='' || !document.getElementById('firstname').value.length){
document.getElementById('errorname').innerHTML="this is an invalid name";
}
}
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="mystyle.css" />
<script type="text/javascript">
function check() {
if (document.myform.firstname.value === null || myform.firstname.value.length == 0) {
document.getElementById('errorname').innerHTML = "this is an invalid name";
}
}
</script>
</head>
<body>
<form name="myform">
<p>name</p>
<input type="text" name="firstname" onblur="check()" /><span id="errorname"> </span>
<br />
<input type="button" value="submit" />
</form>
</body>
</html>

how to pass input value using javascript

I have more than 5 card types and each card as different validation, when user change the card type, the reqpattern will change and i need to pass changed reqpattern in callonkeyup function.
Please tell me how to pass changed reqpattern in callonkeyup function.
callonkeyup(this,'AccountNumber',this.getAttribute('reqpattern'))
<input type="tel" reqpattern="^[0-9]+\$"
onblur="callonkeyup(this,'AccountNumber',this.getAttribute('reqpattern'));"
value="" id="AccountNumber"/>
Why not just pass the req pattern within the function call:
<input type="tel" onblur="callonkeyup(this,'AccountNumber', '^[0-9]+\$');" value="" id="AccountNumber"/>
I am not able to reproduce your issue with your code.
I have tried with the below code snippet, Can you please check it at your hand?
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
</head>
<body>
<input type="tel" reqpattern="^[0-9]+\$"
onblur="callonkeyup(this,'AccountNumber',this.getAttribute('reqpattern'));"
value="" id="AccountNumber"/>
<input type="tel" reqpattern="^[a-z]+\$"
onblur="callonkeyup(this,'AccountName',this.getAttribute('reqpattern'));"
value="" id="AccountName"/>
<script>
function callonkeyup(obj,txtType,rpattern)
{
alert(obj);
alert(txtType);
alert(rpattern);
}
</script>
</body>
</html>
Let me know if any concern.

Create a "calculation form" in javascript

so I have a project now, as an addition to my company's website, we want to show our clients how much they can save on gas by buying one of our electric cars, I'm responsible for making it happen, I don't know how to approach this using javascript, can you guys give me a hand? our marketing guy got the idea from this website, that is basically what we want, but I was hoping i could make it a little better on some aspects:
1st-the client wouldn't have to press submit to see the results, as they fill the last field, the calculated part is populated automatically, for this i've been fiddling with the onChange event, unsuccessfully though xD
here's what I have so far, it is not working, at least on dreamweaver's live mode, haven't tested it online yet as I was hoping to develop the whole thing offline:
<script type="text/javascript">
function calc(){
var km=document.getElementById(km).value;
var euro=document.getElementById(euro).value;
var consumo=document.getElementById(consumo).value;
var cem_km=consumo*euro;
var fossil_day=(cem_km*km)/100;
return fossil_day;
}
</script>
<form name="calc" id="calc" >
<p>
Km/dia
<input type="text" name="km" id="km" value="" />
</p>
<p>
€/Litro
<input type="text" name="euro" id="euro" value="" />
</p>
<p>
Litros/100km
<input type="text" onChange="calc()" name="consumo" id="consumo" value="" />
</p>
<input type="button" onClick="calc()" name="submit" id="submit" value="Calcular" />
<script type="text/javascript">
var fossil_day = calc();
document.write('<p>'+fossil_day+'</p>');
</script>
</form>
Please note that although I have this already, I wouldnt mind not doing this at all and using another solution, even if it doesnt use forms, I'm just showing what i have already so you can tell me how I'm wrong and how I can have a better approach at it
there are many errors inside your code
document.getElementById() needs the element id in brackets ''
you can't create a element with the same name,id as a function calc else it will throw an error as it's an object and not a function.
your executing the function onload... but you want it to be executed when the button is clicked & onchange.
you don't need to add value if empty and name if you use getElementById
return false in the function on buttons inside form else it could send the form and so refresh the page.
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>calc</title>
<script>
function calc(){
var km=document.getElementById('km').value;
var euro=document.getElementById('euro').value;
var consumo=document.getElementById('consumo').value;
var cem_km=consumo*euro;
var fossil_day=(cem_km*km)/100;
document.getElementById('result').innerHTML=fossil_day;
return false
}
</script>
</head>
<body>
<form>
<p>Km/dia<input type="text" id="km"/></p>
<p>€/Litro<input type="text" id="euro" /></p>
<p>Litros/100km<input type="text" onChange="calc()" id="consumo" /></p>
<input type="button" onClick="calc()" value="Calcular" />
</form>
<div id="result"></div>
</body>
</html>
Useing jQuery (and html5 type="number" form fields):
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form>
<p>
Km/dia
<input type="number" name="km" id="km" value="" />
</p>
<p>
€/Litro
<input type="number" name="euro" id="euro" value="" />
</p>
<p>
Litros/100km
<input type="number" name="consumo" id="consumo" value="" />
</p>
<div id="fossil-day"></div>
</form>
<script src="http://codeorigin.jquery.com/jquery-1.10.2.min.js"></script>
<script>
function calculate(){
var km = $('#km').val();
var euro = $('#euro').val();
var consumo = $('#consumo').val();
var cem_km = consumo*euro;
var fossil_day = (cem_km*km)/100;
$('#fossil-day').html(fossil_day);
}
$(function() {
/*when #consumo input loses focus, as per original question*/
$('#consumo').blur(function(){
calculate();
});
});
</script>
</body>
</html>

How to pass text in a textbox to JavaScript function?

Suppose I have the following HTML code, how can I pass the user's input to execute(str) JavaScript function as an argument?
<body>
<input name="textbox1" type="text" />
<input name="buttonExecute" onclick="execute(//send the user's input in textbox1 to this function//)" type="button" value="Execute" />
</body>
You could either access the element’s value by its name:
document.getElementsByName("textbox1"); // returns a list of elements with name="textbox1"
document.getElementsByName("textbox1")[0] // returns the first element in DOM with name="textbox1"
So:
<input name="buttonExecute" onclick="execute(document.getElementsByName('textbox1')[0].value)" type="button" value="Execute" />
Or you assign an ID to the element that then identifies it and you can access it with getElementById:
<input name="textbox1" id="textbox1" type="text" />
<input name="buttonExecute" onclick="execute(document.getElementById('textbox1').value)" type="button" value="Execute" />
As opposed to passing the text as a variable, you can use the DOM to retrieve the data in your function:
var text = document.getElementsByName("textbox1").value;
You could just get the input value in the onclick-event like so:
onclick="execute(document.getElementById('textbox1').value);"
You would of course have to add an id to your textbox
This is what I have done. (Adapt from all of your answers)
<input name="textbox1" type="text" id="txt1"/>
<input name="buttonExecute" onclick="execute(document.getElementById('txt1').value)" type="button" value="Execute" />
It works. Thanks to all of you. :)
if I have understood correct the question :
<!DOCTYPE HTML>
<HEAD>
<TITLE>Passing values</TITLE>
<style>
</style>
</HEAD>
Give a number :<input type="number" id="num"><br>
<button onclick="MyFunction(num.value)">Press button...</button>
<script>
function MyFunction(num) {
document.write("<h1>You gave "+num+"</h1>");
}
</script>
</BODY>
</HTML>
document.getElementById('textbox1').value
You can get textbox value and Id by the following simple example in dotNet programming
<html>
<head>
<script type="text/javascript">
function GetTextboxId_Value(textBox)
{
alert(textBox.value); // To get Text Box Value(Text)
alert(textBox.id); // To get Text Box Id like txtSearch
}
</script>
</head>
<body>
<input id="txtSearch" type="text" onkeyup="GetTextboxId_Value(this)" /> </body>
</html>

Categories