Javascript function into input - javascript

<form name="test_form">
<input id="demo" type="text" value="">
</form>
<button onclick="data()">GO</button>
<script>
var x=document.getElementById("demo");
function data()
{
x.innerHTML="TEST";
}
</script>
I am trying to input JavaScript function into input field but I've got a problem with that, nothing is happening, any ideas how to fix it?

x.innerHTML="TEST";
should be
x.value="TEST";

<script>
function data()
{
var x=document.getElementById("demo");
x.value="TEST";
}
</script>
<form name="test_form">
<input id="demo" type="text" value="">
</form>
<button onclick="data();">GO</button>
http://jsfiddle.net/vdAY7/

Related

On button click focus input in JavaScript

I am trying to autofocus input on button click but it doesn't work. Code can be found here for edit: https://www.w3schools.com/code/tryit.asp?filename=FPPCFP5RTU9E
<input type="text" id="myText" autofocus>
<button onclick="myFunction()">Focus Input</button>
<p id="demo"></p>
<script>
function myFunction() {
document.getElementById("myText").autofocus = true;
}
</script>
autofocusis an HTML attribute. If you want to set the focus property by JavaScript you have to use focus().
So try document.getElementById("myText").focus();:
Working Code:
function myFunction() {
document.getElementById("myText").focus();
}
<input type="text" id="myText" autofocus>
<button onclick="myFunction()">Focus Input</button>
<p id="demo"></p>

Send parameter to JS function with input form

i have this js function
function showThisTag(tag) {
$('#content .blog').not("."+tag).hide();
}
how can i send the "tag" parameter from a form?
something like
<form name="input" onSubmit="showThisTag(this)">
<input type="text" >
<input type="submit" value="sendit">
</form>
thanks!
Here's an example how you can do it:
<html>
<head>
<script type="text/javascript">
function myfunction(txt) {
alert(txt)
}
</script>
</head>
<body>
<form>
<input id="arg"/>
<input type="button" value="submit" onclick="myfunction(document.getElementById('arg').value)"/>
</form>
</body>
</html>
Put the value you want to send in quotes:
<form name="input" onSubmit="showThisTag('classToShow')">
If you want get the class from the input field, give it a class so you can access it and get its value:
function showThisTag(form) {
var tag = $(form).find(".tagclass").val();
$('#content .blog').not("."+tag).hide();
$("#content .blog."+tag).show();
return false;
};
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form name="input" onSubmit="return showThisTag(this)">
<input type="text" class="tagclass">
<input type="submit" value="sendit">
</form>
<div id="content">
<div class="blog one">
Blog post one
</div>
<div class="blog two">
Blog post two
</div>
</div>

Form is submitting even if javascript function return false

Javascript validation is not working! Form is submitting even if function returns false. Where i should make changes? Here is the part of the code i wrote.
<html>
<head>
<script>
function check()
{
temp=document.getElementById("name").value;
if(temp=="")
{
document.getElementById("err").innerHTML="error found";
document.getElementById("name").focus();
return false;
}
}
</script>
</head>
<body>
<form>
<input type="text" id="name">
<div id="err"></div>
<input type="submit" onClick="check()">
</form>
</body>
</html>
I would suggest you to use onSubmit event of form instead of onClick event of button.
You also need to use return with HTML onevent attributes
HTML
<form onSubmit="return check()">
<input type="text" id="name"/>
<div id="err"></div>
<input type="submit" />
</form>
DEMO

preview before submit

can anybody tell me how to make preview before submit in one form using javascript in php
i have an example like
<script type="text/javascript">
$(document).ready(function() {
`$('#db').hide();
});
function preview()
{
var add=document.getElementById('Address_Street').value;
if(add.trim()!="")
{
$('#db').show();
document.getElementById('p_Address_Street').value=add;
}
}
<body>
<form name="approval" id="approval" method="post" enctype="multipart/form-data" action="">
<input type="text" name="Address_Street" id="Address_Street" size="36" value="">
<input type="submit" value="Submit" >`
<input type="button" value="Preview" onclick="preview()">
</form>
<div id="db">
Address Street: <input type="text" name="p_Address_Street" id="p_Address_Street" size="36" value="" readonly="readonly">
</div>
</body>
can everybody tell me the right one please :(
Add id to your Preview button
<input id="Preview" type="button" value="Preview">
Then try this Js
$(document).ready(function() {
$('#db').hide();
$('#Preview').on('click', function(){
var add=$("#Address_Street").val();
if(add.trim()!="")
{
$('#db').show();
$('#p_Address_Street').val(add);
}
});
});
You have an error on this line
`$('#db').hide(); try to remove the ` char
And then, if you're using JQuery, stick to it.
var add = $('#Address_Street').val();
To debug, remove the if statement. Maybe the error is there.
The example:
http://jsfiddle.net/fa295/

java script onclick function not working

<title>Javascript</title>
<script>
function buttonreport(id,name,address){
var userid ="Id:"+id;
var username="Name :"+name+"\n";
var useraddress ="Address:"+address+"\n";
alert(userid+username+useraddress);
}
</script>
</head>
<body>
ID:<input type="text" name="id"/><br/>
NAME:<input type="text" name="name"/><br/>
ADDRESS:<input type="text" name="address"/><br/>
<input type="submit" value="Submit" onclick="buttonreport(this.id,this.name,this.address)"/>
</body>
now its working but output shows as id:name:address no entered values are displayed
You spelled alert wrong in your script
I'm not JS expert, but maybe You need <form> tags around submit form. You could also just use getelementbyid.
your alret should be alert. now it works :)
<title>Javascript</title>
<script>
function buttonreport(id,name,address){
var userid ="Id:"+id;
var username="Name :"+name+"\n";
var useraddress ="Address:"+address+"\n";
alert(userid+username+useraddress);
}
</script>
</head>
<body>
ID:<input type="text" name="id"/><br/>
NAME:<input type="text" name="name"/><br/>
ADDRESS:<input type="text" name="address"/><br/>
<input type="submit" value="Submit" onclick="buttonreport(this.id,this.name,this.address)"/>

Categories

Resources