I have 2 forms with the same ids.
I want when you type a key in the text area with id=ingevuldNotitie then javascript will submit the form. Is there a way to do this and so how?
<div id="response">
<div id="child_response">
<form action="test.php" method="post" id="notitieForm" onsubmit="return notitieOpslaan();">
<input type="text" id="clientNotitieDatum" value="2022-10-17" name="clientNotitieDatum" hidden="">
<textarea rows="4" cols="50" id="ingevuldNotitie" name="ingevuldNotitie"></textarea>
<input type="submit" name="clientNotitie" value="Notitie opslaan">
</form>
<form action="test.php" method="post" id="notitieForm" onsubmit="return notitieOpslaan();">
<input type="text" id="clientNotitieDatum" value="2022-10-18" name="clientNotitieDatum" hidden="">
<textarea rows="4" cols="50" id="ingevuldNotitie" name="ingevuldNotitie"></textarea>
<input type="submit" name="clientNotitie" value="Notitie opslaan">
</form>
</div>
</div>
<script>
// I tried this
$("#response").bind("keyup", function () {
$(this).closest('[name=clientNotitieDatum]').submit();
});
// And I tried this
$("text[name='ingevuldNotitie']").bind("keyup", function () {
$(this).closest('form').submit();
});
</script>
Because the elements are dynamically added via ajax, you should use $(document).on('keyup', "textarea[name='ingevuldNotitie']", function() {.
Alo, in your forms onsubmit, add an argument to the function:
onsubmit="return notitieOpslaan(this);"
And use that argument to get the form's data, such as clientNotitieDatum.
See working demo below:
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="response">
<div id="child_response">
<form action="test.php" method="post" id="notitieForm" onsubmit="return notitieOpslaan(this);">
<input type="text" id="clientNotitieDatum" value="2022-10-17" name="clientNotitieDatum" hidden="">
<textarea rows="4" cols="50" id="ingevuldNotitie" name="ingevuldNotitie"></textarea>
<input type="submit" name="clientNotitie" value="Notitie opslaan">
</form>
<form action="test.php" method="post" id="notitieForm" onsubmit="return notitieOpslaan(this);">
<input type="text" id="clientNotitieDatum" value="2022-10-18" name="clientNotitieDatum" hidden="">
<textarea rows="4" cols="50" id="ingevuldNotitie" name="ingevuldNotitie"></textarea>
<input type="submit" name="clientNotitie" value="Notitie opslaan">
</form>
</div>
</div>
<script>
$(document).on('keyup', "textarea[name='ingevuldNotitie']", function() {
$(this).closest('form').submit();
});
function notitieOpslaan(theForm) {
var clientNotitieDatum = theForm['clientNotitieDatum'].value;
console.log('form would have been submitted', clientNotitieDatum);
return false;
}
</script>
Related
my code below deletes the text from my textarea that is inserted by a user after clicking the submit button but what i want is i want the text to be in my textarea after submit.
<form action="/search" name="myform" id="formid" method="post" >
<div class="input-group" >
<!--
<textarea name="input" id="textArea" />
<button type="submit" onclick="return fill()" >Search</button>
</button>
</div>
</form>
<script type="text/javascript">
function fill()
{
var x = document.getElementById('textArea').value;
document.getElementById('demo').innerHTML=x;
}
</script>
Simply try to add onSubmit="return false;" at the form tag
<form action="/search" onSubmit="return false;" name="myform" id="formid" method="post" >
I am new to javascript, I was creating a form and taking the value from the text box but to cross-check it whether the value is fetched, I used alert inside my js code. it is not showing any alert box while running the code.
I tried to use it without form tag it was working fine.
<html>
<body>
<h2>JavaScript Alert</h2>
<form name="myForm" action="thanks.html" method="get">
UserName:<br/><input type="text" name="user"><br/><br/>
Email:<br/><input type="text" name="pass" id="email"><br/><br/>
Message:<br/><textarea name="msg" rows="5"></textarea><br/><br/>
<input type="submit" name="submit" value="Submit Form" onsubmit="validate()">
</form>
<script>
function validate(){
var email= document.getElementById("email").value;
alert(email);
}
</script>
</body>
</html>
can you please tell me what I am doing wrong
input element doesn't have onSubmit event but form does.
That's all you need to change:
<form name="myForm" action="thanks.html" method="get" onsubmit="validate()">
Demo:
function validate() {
var email = document.getElementById("email").value;
alert(email);
}
<h2>JavaScript Alert</h2>
<form name="myForm" action="thanks.html" method="get" onsubmit="validate()">
UserName:<br/><input type="text" name="user"><br/><br/> Email:
<br/><input type="text" name="pass" id="email"><br/><br/> Message:
<br/><textarea name="msg" rows="5"></textarea><br/><br/>
<input type="submit" name="submit" value="Submit Form">
</form>
Change the inout type from "submit" to "button" and then submit your form from validate function using $("#form_id").submit()
Also you can try adding e.preventDefault() in validate function.
input has no onSubmit event but form has
function validate(){
var email= document.getElementById("email").value;
alert(email);
}
<html>
<body>
<h2>JavaScript Alert</h2>
<form name="myForm" action="thanks.html" method="get" onsubmit="validate()">
UserName:<br/><input type="text" name="user"><br/><br/>
Email:<br/><input type="text" name="pass" id="email"><br/><br/>
Message:<br/><textarea name="msg" rows="5"></textarea><br/><br/>
<input type="submit" name="submit" value="Submit Form" >
</form>
\
</body>
</html>
You can use onsubmit="validate()" function in form tag instead input type submit.
<html>
<body>
<h2>JavaScript Alert</h2>
<form name="myForm" action="thanks.html" method="get" onsubmit="validate()">
UserName:<br/><input type="text" name="user"><br/><br/>
Email:<br/><input type="text" name="pass" id="email"><br/><br/>
Message:<br/><textarea name="msg" rows="5"></textarea><br/><br/>
<input type="submit" name="submit" value="Submit Form">
</form>
<script>
function validate() {
var email= document.getElementById( "email" ).value;
alert( email );
}
</script>
</body>
</html>
I inserted a form on a html page, which contains several fields the user is supposed to fill ("name", "surname", a textarea, and so on); I then added a button and assigned the id Click to it:
<input type="button" name="button" value="Click" id="cliccami">
I linked the html page to a JQuery file.
By using JQuery I want to add a function clearAll which clears all the fields in the form when the user clicks on the button. I want to use another function main which calls the function clearAll when the button is clicked, as in the following:
function main() {
$("form").on("button", clearAll);
}
How can I write the function clearAll (to empty the fields in the form) by using JQuery?
Inside the function you can target the closest() form of this element to find all the elements to clear them like the following way:
function main() {
$("form").on("click", "#cliccami", clearAll);
}
function clearAll() {
$(this).closest("form").find("input[type=text], textarea").val("");
}
function main() {
$("form").on("click", "#cliccami", clearAll);
}
main();
function clearAll() {
$(this).closest("form").find("input[type=text], textarea").val("");
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
<form>
<div>
<label>First Name:</label>
<input type="text" id="first-name">
</div>
<div>
<label>Last Name:</label>
<input type="text" id="last-name">
</div>
<div>
<label>Comments:</label>
<textarea id="comments"></textarea>
</div>
<input type="button" name="button" value="Click" id="cliccami">
</form>
Use jQuery's .val() function to set the value of a input element. Check the snippet for an example:
function clearAll() {
$(":input[type=text]").val("");
}
function main() {
$("#cliccami").on("click", clearAll);
}
main();
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" value="test">
<input type="button" name="button" value="Click" id="cliccami">
You can bind your function using the onclick attribute. And use reset method to reset the entire form this way.
function main(){
// reset form
$('#my-form')[0].reset();
// do whatever you want
alert('Form cleared!');
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
<div class="container">
<h2>Form</h2>
<form id="my-form">
<div class="form-group">
<label for="first-name">First Name:</label>
<input type="text" class="form-control" id="first-name">
</div>
<div class="form-group">
<label for="last-name">Last Name:</label>
<input type="text" class="form-control" id="last-name">
</div>
<div class="form-group">
<label for="comments">Comments:</label>
<textarea class="form-control" id="comments"></textarea>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
<button type="button" onclick="main()" class="btn btn-default">Clear All</button>
</form>
</div>
Try this
<form id="form1" action="">
<input type="text">
<input type="text">
<textarea name="" id="" cols="30" rows="10"></textarea>
<input type="button" name="button" value="Click" id="cliccami">
</form>
function clearAll() {
$('#form1 input, #form1 textarea, #form1 select').each(function() {
$(this).val('');
});
}
$("#cliccami").on('click',function(){
clearAll();
});
$('#clearit').click(function(){
$(":input[type=text]").val("");
setTimeout(function(){ afteremptyfield(); }, 1000);
})
function afteremptyfield(){
alert("Function trigged after clearing fields")
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" value="test">
<input type="button" name="button" value="Clear and trigger a function" id="clearit">
This is how it could be done....
I am having this situation with many forms (for instance I show you a coupe of them):
<form method="POST" enctype="multipart/form-data">
<textarea name="content" id="content" class="form-control" required=""></textarea>
<input name="code" id="code" type="hidden" value="1180224194">
<input name="postId" id="postId" type="hidden" value="167">
<button class="btn btn-commenta">Say something</button>
</form>
<form method="POST" enctype="multipart/form-data">
<textarea name="content" id="content" class="form-control" required=""></textarea>
<input name="code" id="code" type="hidden" value="95959622661">
<input name="postId" id="postId" type="hidden" value="144">
<button class="btn btn-commenta">Say something</button>
</form>
And I have some javascript like this:
<script type="text/javascript">
$("form").submit(function (e) {
e.preventDefault();
var comment = document.getElementById("content").value;
var postId = document.getElementById("postId").value;
var code = document.getElementById("code").value;
if(comment && postId && code){
$.ajax({
type: 'POST',
...SOME AJAX
});
}
else {
console.log("error");
console.log(comment);
console.log(postId);
console.log(code);
}
return false;
});
</script>
Everything works fine when I have a single form (or when I use the first one), but when I try to get the values of the inputs in a different form than the first one, I get an error and my fields are empty.
How can I tell the script to select the values (with getElementById) of the submitted form? I tried with "this" but the console tells me "Cannot read property 'getElementById' of undefined".
The id attribute should be unique in the same document, use common classes instead like :
<form method="POST" enctype="multipart/form-data">
<textarea name="content" class="form-control content" required=""></textarea>
<input name="code" class="code" type="hidden" value="1180224194">
<input name="postId" class="postId" type="hidden" value="167">
<button class="btn btn-commenta">Say something</button>
</form>
Then get the element values using this to refer to the curren form :
var comment = $(".content", this).val();
var postId = $(".postId", this).val();
var code = $(".code", this).val();
Hope this helps.
$("form").submit(function(e) {
e.preventDefault();
var comment = $(".content", this).val();
var postId = $(".postId", this).val();
var code = $(".code", this).val();
console.log(comment);
console.log(postId);
console.log(code);
if (comment && postId && code) {
console.log("ajax query");
} else {
console.log("error");
}
return false;
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form method="POST" enctype="multipart/form-data">
<textarea name="content" class="form-control content" required=""></textarea>
<input name="code" class="code" type="hidden" value="1180224194">
<input name="postId" class="postId" type="hidden" value="167">
<br>
<button class="btn btn-commenta">Say something</button>
</form>
<form method="POST" enctype="multipart/form-data">
<textarea name="content" class="form-control content" required=""></textarea>
<input name="code" class="code" type="hidden" value="95959622661">
<input name="postId" class="postId" type="hidden" value="144">
<br>
<button class="btn btn-commenta">Say something</button>
</form>
Try using different id's for different elements. Id's should always be unique. If you want to use the same name for multiple elements (e.g for styling) use a class.
I suggest changing your ID's to something like:
<form method="POST" enctype="multipart/form-data">
<textarea name="content" id="content1" class="form-control" required="">
</textarea>
<input name="code" id="code1" type="hidden" value="1180224194">
<input name="postId" id="postId1" type="hidden" value="167">
`enter code here`<button class="btn btn-commenta">Say something</button>
</form>
<form method="POST" enctype="multipart/form-data">
<textarea name="content" id="content2" class="form-control" required="">
</textarea>
<input name="code" id="code2" type="hidden" value="95959622661">
<input name="postId" id="postId2" type="hidden" value="144">
<button class="btn btn-commenta">Say something</button>
</form>
https://www.w3schools.com/tags/att_global_id.asp W3Schools explains:
"The id attribute specifies a unique id for an HTML element (the value
must be unique within the HTML document).
The id attribute is most used to point to a style in a style sheet,
and by JavaScript (via the HTML DOM) to manipulate the element with
the specific id."
this is my code.jsp file and I've written javascript in it to enable/disable textbox name=uinp when toggling checkbox name=input but it is not working!
<html>
<body>
<form action="compile.jsp" method="get">
Custom Input: <input type="checkbox" name="input" onchange="toggle('input','uinp')"><br><br>
<textarea rows="5" cols="15" name="uinp" style="display: none"></textarea><br><br>
<br><input type="submit" value="Submit">
<input type="reset" value="Cancel">
</form>
<script>
function toggle(chk,txt)
{
if(document.getElementById(chk).checked)
document.getElementById(txt).style.display='';
else
document.getElementById(txt).style.display='none';
}
</script>
</body>
</html>
Please anyone help me with this!
You need to set id for textarea otherwise getElementById() returns null, since there is no element with that id. Also you can pass this context to refer the checkbox.
<form action="compile.jsp" method="get">
Custom Input:
<input type="checkbox" name="input" onchange="toggle(this,'uinp')">
<br>
<br>
<textarea rows="5" cols="15" id="uinp" style="display: none"></textarea>
<br>
<br>
<br>
<input type="submit" value="Submit">
<input type="reset" value="Cancel">
</form>
<script>
function toggle(ele, txt) {
if (ele.checked)
document.getElementById(txt).style.display = '';
else
document.getElementById(txt).style.display = 'none';
}
</script>