I'd like to have two buttons that submit the same value (determined by the input field).
<form action="index.html">
<button type="submit" name="btn1">Btn1</button>
<button type="submit" name="btn2">Btn2</button>
<input type="text" value="50">
</form>
It should result in:
Btn1 => index.html?Btn1=50
Btn2 => index.html?Btn2=50
call index.html according to teh button clicked.
<script type="text/javascript">
function setButton1Value(){
//call index.html?Btn1=50
}
</script>
<script type="text/javascript">
function setButton2Value(){
//call index.html?Btn2=50
}
</script>
<form action="index.jsp" method="get">
<input type="text" value="50" name="input" id="input">
<button type="submit" name="btn1" id="btn1" onclick="setButton1Value()">Btn1</button>
<button type="submit" name="btn2" id="btn2" onclick="setButton2Value()">Btn2</button>
</form>
Two buttons can not send the same value as only one is clicked at a time. For one button to send the value you can try this :-
<form action="index.jsp" method="get">
<input type="text" value="50" name="input" id="input">
<button type="submit" name="btn1" id="btn1" onclick="putValue()">Btn1</button>
<button type="submit" name="btn2" id="btn2" onclick="putValue()">Btn2</button>
</form>
<script type="text/javascript">
function putValue(){
document.getElementById('btn1').value=document.getElementById('input').value;
document.getElementById('btn2').value=document.getElementById('input').value;
}
</script>
Related
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 want to have the btnDisableFilteredList disabled during the initial page load. Then I want to enabled the enable it after the btnSearch button is click. How do I do it using jQuery?
<div class="panel panel-heading">
<form id="frmAutoCritical">
Search: <input type="text" name="SearchString" /><input type="submit" id="btnSearch" value="Filter" />
<input type="button" id="btnDisableFilteredList" value="Disable Filtered List" />
</form>
</div>
You need to give the btnDisableFilteredList the disabled prop first:
disabled="disabled"
Then setup an on click event listener on the btnSearch input to remove the disabled prop from the btnDisableFilteredList input.
$(function() {
$("#btnSearch").on("click", (e) => {
e.preventDefault();
$("#btnDisableFilteredList").prop("disabled", false);
});
});
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<div class="panel panel-heading">
<form id="frmAutoCritical">
Search: <input type="text" name="SearchString" />
<input type="submit" id="btnSearch"value="Filter"/>
<input type="button" id="btnDisableFilteredList" value="Disable Filtered List" disabled="disabled"/>
</form>
Try in this way:
$(document).ready(function() {
$("#btnSearch").click(function () {
$("#btnDisableFilteredList").css("display", "inline-block");
});
});
#btnDisableFilteredList {
display: none;
}
<div class="panel panel-heading">
<form id="frmAutoCritical">
Search:
<input type="text" name="SearchString" />
<input type="button" id="btnSearch" value="Filter" />
<input type="button" id="btnDisableFilteredList" value="Disable Filtered List" />
</form>
</div>
Look at this codepen
I used type="button" instead of type="submit" to avoid reloading the page, then to submit the form you can use jQuery's AJAX Function
I want to show a tooltip when the user didn't click the specific button while submitting a form. I prevent the form from submitting and I want to show just the tooltip to say "you should click first the button". Thank's in advance.
<form id="signUpForm">
<button type="button" id="agree" class="btn btn-default" data-color="info" tabindex="7">I Agree</button>
<input type="hidden" id="hidden" value="0">
<input type="submit" id="btn-submit" value="submit">
</form>
<script>
$('#signUpForm').submit(function(e) {
if ($('#hidden').val() == '0') {
e.preventDefault();
//show the tooltip that "you first click the agree button" and fadeOut(1000)
}
});
</script>
try this, make button type as button instead of submit:
<form id="signUpForm" >
<button type="button" id="agree" class="btn btn-default" data-color="info" tabindex="7" >I Agree</button>
<input type="hidden" id="hidden" value="0">
<input type="button" id="btn-submit" value="submit">
</form>
<script>
$('#btn-submit').click(function(e){
if($('#hidden').val()=='0'){
}
else {
$('#signUpForm').submit();
}
});
</script>
Check below script it will helpful to you for display the tooltip.
<script>
$(".tooltip").hide();
$('#signUpForm').submit(function (e) {
if ($('#hidden').val() == '0') {
e.preventDefault();
//show the tooltip that "you first click the agree button" and fadeOut(1000)
$("#signUpForm").find(".tooltip").stop().fadeIn();
setTimeout(function () {
$("#signUpForm").find(".tooltip").stop().fadeOut();
}, 1000);
}
});
</script>
HTML Part
<form id="signUpForm">
<button type="button" id="agree" class="btn btn-default" data-color="info" tabindex="7">I Agree</button>
<input type="hidden" id="hidden" value="0">
<input type="submit" id="btn-submit" value="submit">
<div class="tooltip">
you first click the agree button
</div>
</form>
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>
I need to create a radio button form where the selection would redirect you to a different index.html link when a Continue button is pressed. I cant seem to find an easy way to do this. Help!!
Basically as follows:
<form>
<input type="RADIO" name="button" value="^button1^" checked>this button goes to index1.html (Default)<BR></BR>
<input type="RADIO" name="button" value="^button2^">this button goes to index2.html<BR> </BR>
<input type="RADIO" name="button" value="^button3^">this button goes to index3.html<BR> </BR>
<input type="submit" value="Continue">
</form>
In html add a data-href attribute for each button and ID to submit button
<form>
<input type="RADIO" name="button" value="^button1^" data-href="index1.html" checked>this button goes to index1.html (Default)<BR></BR>
<input type="RADIO" name="button" value="^button2^" data-href="index2.html">this button goes to index2.html<BR> </BR>
<input type="RADIO" name="button" value="^button3^" data-href="index3.html">this button goes to index3.html<BR> </BR>
<input type="submit" value="Continue" id="btnFormSubmit">
</form>
Javascript:
var submit = document.getElementById('btnFormSubmit');
submit.addEventListener('click', submitForm);
function submitForm(event){
event.preventDefault();
event.stopPropagation();
var href = '',
inputs = this.parentNode.getElementsByTagName('input')
for(var i=0;i<inputs.length;i++){
if(inputs[i].getAttribute('name') == 'button' && inputs[i].checked){
href = inputs[i].getAttribute('data-href');
window.location = href;
}
}
}
Using jQuery will be like that, add the url to the value field of the radio.
<form method="post">
<input type="RADIO" name="button" value="index1.html" checked>this button goes to index1.html (Default)<BR>
<input type="RADIO" name="button" value="index2.html">this button goes to index2.html<BR>
<input type="RADIO" name="button" value="index3.html">this button goes to index3.html<BR>
<input type="submit" value="Continue">
</form>
jQuery code:
$(function(){
$("form").submit(function(e) {
e.preventDefault();
window.location = $(this).find('input[type="radio"]:checked').val();
});
});
Live example:
http://jsfiddle.net/nnEny/
This might be too obvious but
<form>
<input type="RADIO" name="button" value="^button1^" checked>this button goes to index1.html (Default)<BR></BR>
</form>
<form>
<input type="RADIO" name="button" value="^button2^">this button goes to index2.html<BR> </BR>
</form>
<form>
<input type="RADIO" name="button" value="^button3^">this button goes to index3.html<BR> </BR>
<input type="submit" value="Continue">
</form>