I'm trying to code a checkbox that must be checked in order for the "Submit" button on a form to be enabled. I first tried using a linked file to no avail; now even inserting the script doesn't seem to work. Can anyone help? The entire code for the page is posted below. Thanks in advance!
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="stylesheet.css" />
<script type="text/javascript" src="includes/imagetransition.js"></script>
<script type="text/javascript" src="includes/checkenabledisable.js"></script>
<script type="text/javascript">
var checker = document.getElementById('acknowledgment');
var sendbtn = document.getElementById('submitmessage');
// when unchecked or checked, run the function
checker.onchange = function(){
if(this.checked){
sendbtn.disabled = false;
} else {
sendbtn.disabled = true;
}
}
</script>
<title>Contact Us</title>
</head>
<body>
<div class="page-wrapper">
<div id="header">
<img src="images/img1.jpg" name="slideshow" width="70%" height="200" alt="headerimage.gif"><br>
Previous |
Auto/Stop |
Next</div>
<br><br>
<aside class="sidebar">
<div id="vmenu">
<ul>
<li class="sideli">Home</li>
<li class="sideli">Areas</li>
<li class="sideli">Profiles</li>
<li class="sideli">Contact Us</li>
</ul>
</div>
</aside>
<section class="main">
We will review your submission and contact you, usually within 72 hours.</p>
<form action="actionemail.php" method="post">
<table class="emailtable">
<tr><td><label for="name" class="emaillabel">Name: </label></td><td><input type="text" name="fullname" size="50" value="Name" style="width: 290px;" /></td></tr>
<tr><td><label for="phone" class="emaillabel">Phone: </label></td><td><input type="text" name="phone" size="20" value="Phone" style="width: 290px;" /></td></tr>
<tr><td><label for="email" class="emaillabel">Email: </label></td><td><input type="text" name="email" size="50" value="Email" style="width: 290px;" /></td></tr>
<tr><td><label for="comment" class="emaillabel">Issue: </label></td><td><textarea rows="3" cols="26" name="comment" value="Tell Us More" style="width: 290px; height: 55px"></textarea></td></tr>
</table><br>
<input id="acknowledgment" type="checkbox" name="acknowledgment" value="1" /><label for="acknowledgment">I acknowledge that there may be a delay in responding to this request.</label><br>
<br><input id="submitmessage" type="submit" name ="submitmessage" value="Send Email" disabled />
</form>
</section>
</div>
</body>
</html>
Your function is not "listening" for an onchange event.
Use this instead. Notice I changed the checkbox to have an onclick event.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="stylesheet.css" />
<script type="text/javascript" src="includes/imagetransition.js"></script>
<script type="text/javascript" src="includes/checkenabledisable.js"></script>
<script type="text/javascript">
function enableSubmitButton() {
if (document.getElementById("acknowledgment").checked == true)
document.getElementById("submitmessage").disabled = false;
}
</script>
<title>Contact Us</title>
</head>
<body>
<div class="page-wrapper">
<div id="header">
<img src="images/img1.jpg" name="slideshow" width="70%" height="200" alt="headerimage.gif"><br>
Previous |
Auto/Stop |
Next</div>
<br><br>
<aside class="sidebar">
<div id="vmenu">
<ul>
<li class="sideli">Home</li>
<li class="sideli">Areas</li>
<li class="sideli">Profiles</li>
<li class="sideli">Contact Us</li>
</ul>
</div>
</aside>
<section class="main">
We will review your submission and contact you, usually within 72 hours.</p>
<form action="actionemail.php" method="post">
<table class="emailtable">
<tr><td><label for="name" class="emaillabel">Name: </label></td><td><input type="text" name="fullname" size="50" value="Name" style="width: 290px;" /></td></tr>
<tr><td><label for="phone" class="emaillabel">Phone: </label></td><td><input type="text" name="phone" size="20" value="Phone" style="width: 290px;" /></td></tr>
<tr><td><label for="email" class="emaillabel">Email: </label></td><td><input type="text" name="email" size="50" value="Email" style="width: 290px;" /></td></tr>
<tr><td><label for="comment" class="emaillabel">Issue: </label></td><td><textarea rows="3" cols="26" name="comment" value="Tell Us More" style="width: 290px; height: 55px"></textarea></td></tr>
</table><br>
<input id="acknowledgment" type="checkbox" name="acknowledgment" value="1" onclick="enableSubmitButton()"><label for="acknowledgment">I acknowledge that there may be a delay in responding to this request.</label><br>
<br><input id="submitmessage" type="submit" name ="submitmessage" value="Send Email" disabled />
</form>
</section>
</div>
</body>
</html>
You should place the javascript below the definition of the items. HTML is interpreted linearly. This means that the Javascript is executed in the beginning when no items is known as "acknowledgement"
put some function name inside a script
<script type="text/javascript">
function checker()
{
var checker = document.getElementById('acknowledgment');
var sendbtn = document.getElementById('submitmessage');
// when unchecked or checked, run the function
checker.onchange = function(){
if(this.checked){
sendbtn.disabled = false;
} else {
sendbtn.disabled = true;
}
}
}
</script>
and then you will call function in button onClick event
<form action="actionemail.php" method="post">
<br><input id="submitmessage" type="submit" name ="submitmessage" value="Send Email" disabled onclick="checker();"/>
Related
I have this table with some dependent information that uses Javascript and there is an Add and check button to insert elements dynamically in the table and a delete button for each row to delete. When I click the "add and check" button, a set of rows gets added to the table.
Here is what I have:
Javascript and HTML Code...
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Game</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="description" content="" />
<meta name="keywords" content="" />
<meta name="robots" content="index,follow" />
<link rel="stylesheet" type="text/css" href="styles.css" />
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
</head>
<body>
<div class="contanier">
<div class="row">
<div class="col-sm-6" style="padding:20px; border-right:1px solid #000;">
<div class="form-group">
<label for="email">First Number</label>
<input type="number" name="number1" class="form-control" id="number1" min="1">
</div>
<div class="form-group">
<label for="pwd">Second Number</label>
<input type="number" name="number2" class="form-control" id="number2" min="1" >
</div>
<div class="form-group">
<label for="pwd">Amount</label>
<input type="number" name="amount" class="form-control" id="amount" min="1" >
</div>
<button type="button" id="addbtn" class="btn btn-primary">Add and Check</button>
</div>
<div class="col-sm-6">
<h4 class="text-center">Result</h4>
<div id="main_box">
<div>
<input type="checkbox" name="style" value="classical" onchange="add()">
<label>Remove Doublets</label></div>
<table id="result" class="table table-striped">
<tbody class="tbody">
<tr>
<th>Number</th><th>Amount</th><th>Action</th>
</tr>
</tbody>
</table>
<h3>Total Amount:<span id="totl">0</span></h3>
<form action="" method="post" >
<div id="inputs">
</div>
<input type="hidden" name="user_id" value="22">
<input type="hidden" name="game_id" value="111">
<input type="submit" name="save_data" class=" btn btn-danger" value="SAVE" >
</form>
</div>
</div>
</div>
</div>
<script>
var indx=1;
var total_amount = 0;
$("#addbtn").click(function(){
var number = jQuery('#number1').val();
var number2 = jQuery('#number2').val();;
var amount = parseInt(jQuery('#amount').val());
if(number!="" && number2!="" && amount!="")
{
const arr_num_first = Array.from(String(number));
const arr_num_second = Array.from(String(number2));
for (let numOf_f_array of arr_num_first)
{
for (let numOf_s_array of arr_num_second)
{
total_amount = total_amount+amount;
var new_number = numOf_f_array+""+numOf_s_array;
$('.tbody').append("<tr class='input"+indx+"' ><td>"+new_number+"</td><td>"+amount+"</td><td><button class='dlt' data-am='"+amount+"' data-c='input"+indx+"'>X</button></td><tr>");
$('#inputs').append("<input type='hidden' name='num[]' value='"+new_number+"' class='input"+indx+"' >");
$('#inputs').append("<input type='hidden' name='amount[]' value='"+amount+"' class='input"+indx+"' >");
indx++;
}
}
$('#totl').html(total_amount);
}else{ alert('Fill all fields')}
});
$( "#main_box" ).on( "click",".dlt", function() {
var classname = $(this).attr('data-c');
var dlt_amount = parseInt($(this).attr('data-am'));
var new_total = total_amount-dlt_amount;
total_amount = new_total;
$('#totl').html(new_total);
$('.'+classname).remove();
});
</script>
</body>
</html>
How would one display an incorrect or correct words beside a form in any colour beside the field box when typing? I'm trying to make it so it gives me a real time correct and incorrect when I type in values that match the JS rule.
It should give a real time correct or incorrect beside the box and if it matches the rule then it displays correct
My HTML:
<!doctype html>
<html lang="en">
<head>
<title> Form Example </title>
<meta charset="utf-8">
<link href="keyupform.css" rel="stylesheet">
<script src="prototype.js"></script>
<script src="formkeyup.js"></script>
</head>
<body>
<div class="box" >
<form id="myForm" action="http://www.eecs.yorku.ca/~mbrown/EECS1012/testForm.php" method="get">
<!-- user id -->
<h2> Enter Info </h2>
<p> <span class="fieldName">UserID: </span>
<input type="text" id="userid" name="userid" class="input">
<span class="message"></span></p>
<!-- -->
<p style="text-align:center" class="types"> Enter Code: EECS, ESSE, MUTH, HIST, CHAP, BIO </p>
<p> <span class="fieldName"> Codes </span>
<input type="text" id="code" name="code" class="input">
<span class="message"></span></p>
<!-- Number -->
<p> <span class="fieldName"> Course Num (XXXX): </span>
<input style="width: 4em;" id="number" type="text" name="number" class="input">
<span class="message"></span></p>
<hr>
<p style="text-align:center;"> <button id="submitButton" type="button" onclick="submitbtn"> Submit </button> <input id="clear" type="reset" value="Clear"> </p>
<p style="text-align:center;" id="formError"> <p>
</form>
</div>
</body>
</html>
JS:
window.onload = function() {
$("userid").observe("keyup", enforceID);
$("code").observe("keyup", enforcecode);
$("number").observe("keyup", enforcenumbers);
$("submitButton").observe("click", submitbtn);
}
function enforceID() {
// fucntion must start with a letter and can be any number or letter after
var re = /^[A-Z][A-Z][0-9]+/i;
}
function enforcecode() {
// Only can use these Codes
var codes = ["EECS", "ESSE", "MUTH", "HIST", "CHAP", "BIO"];
var codeType = $("codeType").value;
codeType = codeType.toUpperCase();
}
function enforcenumbers() {
//Only 4 numbers allowed
var re = /^[0 -9][0 -9][0 -9][0 -9]$/
}
You can trigger the form validation on keydown event.
const form = document.getElementById('form');
document.getElementById('userid').addEventListener('keydown', event => {
form.reportValidity();
}, false);
document.getElementById('code').addEventListener('keydown', event => {
form.reportValidity();
}, false);
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Title</title>
</head>
<body>
<form id="form">
<label for="userid">User ID:</label>
<input type="text" id="userid" name="userid" pattern="[A-Z]{2}\d+">
<br />
<label for="code">Code:</label>
<input type="text" id="code" pattern="EECS|ESSE|MUTH|HIST|CHAP|BIO" />
</form>
</body>
</html>
I want to change the src of an image element, for that I use this code, but it doesn't work when I click "add" button, please help.....
<html>
<head>
<meta charset="UTF-8">
<title>Edit Home Page</title>
<script type='text/javascript'>
function setimgsrc()
{
$('#image').attr("src",document.getElementById("src").val());
};
</script>
</head>
<body>
<div class='center-block'>
<label >course title: </label>
<input type="text" id="title" name="title" placeholder="enter title for this course" >
<br> <label >course description: </label>
<input type="text" id="des" name="des" placeholder="enter decription for this course" >
<br> <label >image source : </label>
<input type='text' id='src' name='src' >
<input type='button' class='btn' onclick='setimgsrc()' value='add'> <br> <img id='image' style='width:500px; height:500px' > </div>
</body>
</html>
You did not include jquery library.
your head section should be
<script src="https://code.jquery.com/jquery-1.11.3.js"></script>
<script type='text/javascript'>
function setimgsrc()
{
var getImage = $('#src').val();
$('#image').attr("src", getImage);
};
</script>
If you do not want to use jquery then you should rewrite setimgsrc() function as below
function setimgsrc()
{
var getImage = document.getElementById("src").value;
document.getElementById("image").src = getImage;
};
You haven't added jquery library and here is the updated code:
<html>
<head>
<meta charset="UTF-8">
<title>Edit Home Page</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script type='text/javascript'>
function setimgsrc()
{
$('#image').attr("src",$("#src").val());
};
</script>
</head>
<body>
<div class='center-block'>
<label >course title: </label>
<input type="text" id="title" name="title" placeholder="enter title for this course" >
<br> <label >course description: </label>
<input type="text" id="des" name="des" placeholder="enter decription for this course" >
<br> <label >image source : </label>
<input type='text' id='src' name='src' >
<input type='button' class='btn' onclick='setimgsrc()' value='add'>
<br>
<img id='image' style='width:500px; height:500px' >
</div>
</body>
</html>
I have a function with input var is name of Form and ID of input. Now I want to get the value of password input. The problem is I have some input with same ID but in different form. So I want a flexible way to get its value when I know its form.
I have tried
var x = Form_Name.ID_Name.value;
but it doesn't work. So please tell me how to get the value of password input by a flexible way. Thanks so much. Below is my code:
<html>
<head>
<meta http-equiv="content-type" content="text/html" />
<meta name="author" content="lolkittens" />
<script charset="utf-8" type="text/javascript">
function check(Form_Name, ID_Name)
{
alert(document.getElementById(ID_Name).value);
alert(Form_Name);
var x = Form_Name.ID_Name.value;
alert(x);
}
</script>
<title>Untitled 1</title>
</head>
<body>
<div id="main" style="width: 300px; height: 200px;">
<form name="MainForm" id="MainForm">
<input id="ID" name="ID" type="text" size="20"/>
<input id="Pass" name="Pass" type="password" size="20"/>
</form>
<button value="click Me" onclick="check('MainForm','ID')"> Click Me </button>
</div>
</body>
</html>
I have created another button for you to get password.
<html>
<head>
<meta http-equiv="content-type" content="text/html" />
<meta name="author" content="lolkittens" />
<script charset="utf-8" type="text/javascript">
function check(Form_Name, ID_Name)
{
alert(document.getElementById(ID_Name).value);
}
</script>
<title>Untitled 1</title>
</head>
<body>
<div id="main" style="width: 300px; height: 200px;">
<form name="MainForm" id="MainForm">
<input id="ID" name="ID" type="text" size="20"/>
<input id="Pass" name="Pass" type="password" size="20"/>
</form>
<button value="click Me" onclick="check('MainForm','ID')"> Click Me for ID </button>
<button value="click Me" onclick="check('MainForm','Pass')"> Click Me for Password</button>
</div>
</body>
</html>
Optimized way:
You can achieve it using the same button as well, by passing an array of ID's.
Let me know, I can help more on this.
Access username and password using it's id.
<html>
<head>
<meta http-equiv="content-type" content="text/html" />
<meta name="author" content="lolkittens" />
<script charset="utf-8" type="text/javascript">
function check()
{
var username = document.getElementById("ID").value;
var password = document.getElementById("Pass").value;
alert("Username: "+username+" Password: "+password);
}
</script>
<title>Untitled 1</title>
</head>
<body>
<div id="main" style="width: 300px; height: 200px;">
<form name="MainForm" id="MainForm">
<input id="ID" name="ID" type="text" size="20"/>
<input id="Pass" name="Pass" type="password" size="20"/>
</form>
<button value="click Me" onclick="check()"> Click Me </button>
</div>
</body>
</html>
could you please help me with this code?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Welcome to Daily News</title>
<script type="text/javascript" src="Scripts.js">
</script>
<link href="homepage.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="body">
<div class="header">
<img id="header-image" src="news_logo.png" />
<form id="login-form" action="customerLogin.html" method="post" onsubmit="return validate(this)">
<table>
<tr>
<td><label for="loginid">Login:</label></td>
<td id="login-form"><input id="login-boxes" type="text" id="loginid"></td>
<td><label for="pword">Password:</label></td>
<td id="login-form"><input id="login-boxes" type="password" id="pword"></td>
</tr>
<tr>
<td></td>
<td id="login-buttons"><input type="submit" value=" Sign In ">
<input type="reset" value="Clear form">
</td>
</tr>
</table>
</form>
</div>
</div>
</body>
</html>
My Javascript is:
function validate(loginForm)
{
var booValid = true;
var strErrorMessage = "";
var minLength=5;
var maxLength=10;
if(loginForm.pword.value.length < minLength)
{
strErrorMessage = "password must at least 5 characters\n";
booValid=false;
}
if(loginForm.pword.value.length > maxLength)
{
strErrorMessage = "pasword must not more than 10 characters\n";
booValid=false;
}
if(loginForm.loginid.value.indexOf("#") == -1)
{
strErrorMessage = "please enter an e-mail address as your login name\n";
booValid=false;
}
if(!booValid)
{
alert(strErrorMessage);
}
return booValid;
}
I couldn't figure out what was with this code, it doesn't seem to read from javascript at all.
I have no idea why it does not. I've also tried to use only Login Form and javascript then it works and when i put everything together then it doesn't work.
You probably want to replace every id= with class=
Instead of your later ids you want to use name
<input id="login-boxes" type="text" id="loginid">
<input id="login-boxes" type="password" id="pword">
to
<input class="login-boxes" type="text" name="loginid">
<input class="login-boxes" type="password" name="pword">
Try to change :
<input id="login-boxes" type="password" id="pword">
to
<input id="login-boxes" type="password" name="pword">
and
<input id="login-boxes" type="password" id="pword">
to
<input id="login-boxes" type="password" name="pword">