toggle required field via checkbox javascript
up vote
0
down vote
favorite
I've searched this issue for a while now and this is what I came up with, but it doesnt work:
<form action="Subscription.php" method="post">
<label>field1<a style="color:red">*</a></label>
<input type="text" id="field1" name="field1" pattern="^[A-Za-z -]+$" required>
<input type="checkbox" name="checkbox1" onclick="showMe('more'); togglerequirement();">Do you want more?
<div id="more" style="display:none">
<label>field2<a style="color:red">*</a></label>
<input type="text" id="field2" name="field2" pattern="^[A-Za-z -]+$">
</div>
<input type="submit" value="Invia richiesta di registrazione" class="btn" </form>
<script type="text/javascript">
function showMe (box) {
var chboxs = document.getElementsByName("more");
var vis = "none";
if(chboxs[0].checked) {
vis = "block";
}
document.getElementById(box).style.display = vis;
}
function togglerequirement(){
var checkbox = document.getElementsByName("more");
var field2= document.getElementsByid("field2");
if(checkbox[0].checked){
field2.setAttribute("required", "");
}else{
field2.removeAttribute("required");
}
}
</script>
Before you say anything I've already tried with fiedl2.required; and it doesnt work either way.
What can I do?
javascript html checkbox toggle
add a comment |
up vote
0
down vote
favorite
I've searched this issue for a while now and this is what I came up with, but it doesnt work:
<form action="Subscription.php" method="post">
<label>field1<a style="color:red">*</a></label>
<input type="text" id="field1" name="field1" pattern="^[A-Za-z -]+$" required>
<input type="checkbox" name="checkbox1" onclick="showMe('more'); togglerequirement();">Do you want more?
<div id="more" style="display:none">
<label>field2<a style="color:red">*</a></label>
<input type="text" id="field2" name="field2" pattern="^[A-Za-z -]+$">
</div>
<input type="submit" value="Invia richiesta di registrazione" class="btn" </form>
<script type="text/javascript">
function showMe (box) {
var chboxs = document.getElementsByName("more");
var vis = "none";
if(chboxs[0].checked) {
vis = "block";
}
document.getElementById(box).style.display = vis;
}
function togglerequirement(){
var checkbox = document.getElementsByName("more");
var field2= document.getElementsByid("field2");
if(checkbox[0].checked){
field2.setAttribute("required", "");
}else{
field2.removeAttribute("required");
}
}
</script>
Before you say anything I've already tried with fiedl2.required; and it doesnt work either way.
What can I do?
javascript html checkbox toggle
First of all, there is an issue here : var chboxs = document.getElementsByName("more"); There is ID with this name..
– Alon Shmiel
Nov 7 at 11:25
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I've searched this issue for a while now and this is what I came up with, but it doesnt work:
<form action="Subscription.php" method="post">
<label>field1<a style="color:red">*</a></label>
<input type="text" id="field1" name="field1" pattern="^[A-Za-z -]+$" required>
<input type="checkbox" name="checkbox1" onclick="showMe('more'); togglerequirement();">Do you want more?
<div id="more" style="display:none">
<label>field2<a style="color:red">*</a></label>
<input type="text" id="field2" name="field2" pattern="^[A-Za-z -]+$">
</div>
<input type="submit" value="Invia richiesta di registrazione" class="btn" </form>
<script type="text/javascript">
function showMe (box) {
var chboxs = document.getElementsByName("more");
var vis = "none";
if(chboxs[0].checked) {
vis = "block";
}
document.getElementById(box).style.display = vis;
}
function togglerequirement(){
var checkbox = document.getElementsByName("more");
var field2= document.getElementsByid("field2");
if(checkbox[0].checked){
field2.setAttribute("required", "");
}else{
field2.removeAttribute("required");
}
}
</script>
Before you say anything I've already tried with fiedl2.required; and it doesnt work either way.
What can I do?
javascript html checkbox toggle
I've searched this issue for a while now and this is what I came up with, but it doesnt work:
<form action="Subscription.php" method="post">
<label>field1<a style="color:red">*</a></label>
<input type="text" id="field1" name="field1" pattern="^[A-Za-z -]+$" required>
<input type="checkbox" name="checkbox1" onclick="showMe('more'); togglerequirement();">Do you want more?
<div id="more" style="display:none">
<label>field2<a style="color:red">*</a></label>
<input type="text" id="field2" name="field2" pattern="^[A-Za-z -]+$">
</div>
<input type="submit" value="Invia richiesta di registrazione" class="btn" </form>
<script type="text/javascript">
function showMe (box) {
var chboxs = document.getElementsByName("more");
var vis = "none";
if(chboxs[0].checked) {
vis = "block";
}
document.getElementById(box).style.display = vis;
}
function togglerequirement(){
var checkbox = document.getElementsByName("more");
var field2= document.getElementsByid("field2");
if(checkbox[0].checked){
field2.setAttribute("required", "");
}else{
field2.removeAttribute("required");
}
}
</script>
Before you say anything I've already tried with fiedl2.required; and it doesnt work either way.
What can I do?
javascript html checkbox toggle
javascript html checkbox toggle
edited Nov 7 at 11:27
asked Nov 7 at 11:16
Fabzheimer
174
174
First of all, there is an issue here : var chboxs = document.getElementsByName("more"); There is ID with this name..
– Alon Shmiel
Nov 7 at 11:25
add a comment |
First of all, there is an issue here : var chboxs = document.getElementsByName("more"); There is ID with this name..
– Alon Shmiel
Nov 7 at 11:25
First of all, there is an issue here : var chboxs = document.getElementsByName("more"); There is ID with this name..
– Alon Shmiel
Nov 7 at 11:25
First of all, there is an issue here : var chboxs = document.getElementsByName("more"); There is ID with this name..
– Alon Shmiel
Nov 7 at 11:25
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
You are trying to find checkbox by name "more". But it have name="checkbox1":
<input type="checkbox" name="checkbox1" onclick="showMe('more'); togglerequirement();">
I'ts working fine. But it changes second field to required when it's showing and not required when it's hidind. So, you do not see any effect.
not required here
required here
If I fully understand you, try this:
<form action="Subscription.php" method="post">
<label>field1<a style="color:red">*</a></label>
<input type="text" id="field1" name="field1" pattern="^[A-Za-z -]+$" required>
<input type="checkbox" name="more" onclick="showMe('more'); togglerequirement();">Do you want more?
<div id="more" style="display:none">
<label>field2<a style="color:red">*</a></label>
<input type="text" id="field2" name="field2" pattern="^[A-Za-z -]+$">
</div>
<input type="submit" value="Invia richiesta di registrazione" class="btn" </form>
<script type="text/javascript">
function showMe (box) {
var chboxs = document.getElementsByName("more");
var vis = "none";
if(chboxs[0].checked) {
vis = "block";
}
document.getElementById(box).style.display = vis;
}
function togglerequirement(){
var checkbox = document.getElementsByName("more");
var field2 = document.getElementById("field2");
if(checkbox[0].checked){
field2.setAttribute("required", "");
}else{
field2.removeAttribute("required");
}
}
</script>
still nothing, i've copied it wrong. The actual file is correct but it still doesn't work
– Fabzheimer
Nov 7 at 11:36
You want to toggle required on first field?
– Geckon01
Nov 7 at 11:47
Also, I've added some info to my answer. Read this please
– Geckon01
Nov 7 at 11:53
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
You are trying to find checkbox by name "more". But it have name="checkbox1":
<input type="checkbox" name="checkbox1" onclick="showMe('more'); togglerequirement();">
I'ts working fine. But it changes second field to required when it's showing and not required when it's hidind. So, you do not see any effect.
not required here
required here
If I fully understand you, try this:
<form action="Subscription.php" method="post">
<label>field1<a style="color:red">*</a></label>
<input type="text" id="field1" name="field1" pattern="^[A-Za-z -]+$" required>
<input type="checkbox" name="more" onclick="showMe('more'); togglerequirement();">Do you want more?
<div id="more" style="display:none">
<label>field2<a style="color:red">*</a></label>
<input type="text" id="field2" name="field2" pattern="^[A-Za-z -]+$">
</div>
<input type="submit" value="Invia richiesta di registrazione" class="btn" </form>
<script type="text/javascript">
function showMe (box) {
var chboxs = document.getElementsByName("more");
var vis = "none";
if(chboxs[0].checked) {
vis = "block";
}
document.getElementById(box).style.display = vis;
}
function togglerequirement(){
var checkbox = document.getElementsByName("more");
var field2 = document.getElementById("field2");
if(checkbox[0].checked){
field2.setAttribute("required", "");
}else{
field2.removeAttribute("required");
}
}
</script>
still nothing, i've copied it wrong. The actual file is correct but it still doesn't work
– Fabzheimer
Nov 7 at 11:36
You want to toggle required on first field?
– Geckon01
Nov 7 at 11:47
Also, I've added some info to my answer. Read this please
– Geckon01
Nov 7 at 11:53
add a comment |
up vote
0
down vote
accepted
You are trying to find checkbox by name "more". But it have name="checkbox1":
<input type="checkbox" name="checkbox1" onclick="showMe('more'); togglerequirement();">
I'ts working fine. But it changes second field to required when it's showing and not required when it's hidind. So, you do not see any effect.
not required here
required here
If I fully understand you, try this:
<form action="Subscription.php" method="post">
<label>field1<a style="color:red">*</a></label>
<input type="text" id="field1" name="field1" pattern="^[A-Za-z -]+$" required>
<input type="checkbox" name="more" onclick="showMe('more'); togglerequirement();">Do you want more?
<div id="more" style="display:none">
<label>field2<a style="color:red">*</a></label>
<input type="text" id="field2" name="field2" pattern="^[A-Za-z -]+$">
</div>
<input type="submit" value="Invia richiesta di registrazione" class="btn" </form>
<script type="text/javascript">
function showMe (box) {
var chboxs = document.getElementsByName("more");
var vis = "none";
if(chboxs[0].checked) {
vis = "block";
}
document.getElementById(box).style.display = vis;
}
function togglerequirement(){
var checkbox = document.getElementsByName("more");
var field2 = document.getElementById("field2");
if(checkbox[0].checked){
field2.setAttribute("required", "");
}else{
field2.removeAttribute("required");
}
}
</script>
still nothing, i've copied it wrong. The actual file is correct but it still doesn't work
– Fabzheimer
Nov 7 at 11:36
You want to toggle required on first field?
– Geckon01
Nov 7 at 11:47
Also, I've added some info to my answer. Read this please
– Geckon01
Nov 7 at 11:53
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
You are trying to find checkbox by name "more". But it have name="checkbox1":
<input type="checkbox" name="checkbox1" onclick="showMe('more'); togglerequirement();">
I'ts working fine. But it changes second field to required when it's showing and not required when it's hidind. So, you do not see any effect.
not required here
required here
If I fully understand you, try this:
<form action="Subscription.php" method="post">
<label>field1<a style="color:red">*</a></label>
<input type="text" id="field1" name="field1" pattern="^[A-Za-z -]+$" required>
<input type="checkbox" name="more" onclick="showMe('more'); togglerequirement();">Do you want more?
<div id="more" style="display:none">
<label>field2<a style="color:red">*</a></label>
<input type="text" id="field2" name="field2" pattern="^[A-Za-z -]+$">
</div>
<input type="submit" value="Invia richiesta di registrazione" class="btn" </form>
<script type="text/javascript">
function showMe (box) {
var chboxs = document.getElementsByName("more");
var vis = "none";
if(chboxs[0].checked) {
vis = "block";
}
document.getElementById(box).style.display = vis;
}
function togglerequirement(){
var checkbox = document.getElementsByName("more");
var field2 = document.getElementById("field2");
if(checkbox[0].checked){
field2.setAttribute("required", "");
}else{
field2.removeAttribute("required");
}
}
</script>
You are trying to find checkbox by name "more". But it have name="checkbox1":
<input type="checkbox" name="checkbox1" onclick="showMe('more'); togglerequirement();">
I'ts working fine. But it changes second field to required when it's showing and not required when it's hidind. So, you do not see any effect.
not required here
required here
If I fully understand you, try this:
<form action="Subscription.php" method="post">
<label>field1<a style="color:red">*</a></label>
<input type="text" id="field1" name="field1" pattern="^[A-Za-z -]+$" required>
<input type="checkbox" name="more" onclick="showMe('more'); togglerequirement();">Do you want more?
<div id="more" style="display:none">
<label>field2<a style="color:red">*</a></label>
<input type="text" id="field2" name="field2" pattern="^[A-Za-z -]+$">
</div>
<input type="submit" value="Invia richiesta di registrazione" class="btn" </form>
<script type="text/javascript">
function showMe (box) {
var chboxs = document.getElementsByName("more");
var vis = "none";
if(chboxs[0].checked) {
vis = "block";
}
document.getElementById(box).style.display = vis;
}
function togglerequirement(){
var checkbox = document.getElementsByName("more");
var field2 = document.getElementById("field2");
if(checkbox[0].checked){
field2.setAttribute("required", "");
}else{
field2.removeAttribute("required");
}
}
</script>
<form action="Subscription.php" method="post">
<label>field1<a style="color:red">*</a></label>
<input type="text" id="field1" name="field1" pattern="^[A-Za-z -]+$" required>
<input type="checkbox" name="more" onclick="showMe('more'); togglerequirement();">Do you want more?
<div id="more" style="display:none">
<label>field2<a style="color:red">*</a></label>
<input type="text" id="field2" name="field2" pattern="^[A-Za-z -]+$">
</div>
<input type="submit" value="Invia richiesta di registrazione" class="btn" </form>
<script type="text/javascript">
function showMe (box) {
var chboxs = document.getElementsByName("more");
var vis = "none";
if(chboxs[0].checked) {
vis = "block";
}
document.getElementById(box).style.display = vis;
}
function togglerequirement(){
var checkbox = document.getElementsByName("more");
var field2 = document.getElementById("field2");
if(checkbox[0].checked){
field2.setAttribute("required", "");
}else{
field2.removeAttribute("required");
}
}
</script>
<form action="Subscription.php" method="post">
<label>field1<a style="color:red">*</a></label>
<input type="text" id="field1" name="field1" pattern="^[A-Za-z -]+$" required>
<input type="checkbox" name="more" onclick="showMe('more'); togglerequirement();">Do you want more?
<div id="more" style="display:none">
<label>field2<a style="color:red">*</a></label>
<input type="text" id="field2" name="field2" pattern="^[A-Za-z -]+$">
</div>
<input type="submit" value="Invia richiesta di registrazione" class="btn" </form>
<script type="text/javascript">
function showMe (box) {
var chboxs = document.getElementsByName("more");
var vis = "none";
if(chboxs[0].checked) {
vis = "block";
}
document.getElementById(box).style.display = vis;
}
function togglerequirement(){
var checkbox = document.getElementsByName("more");
var field2 = document.getElementById("field2");
if(checkbox[0].checked){
field2.setAttribute("required", "");
}else{
field2.removeAttribute("required");
}
}
</script>
edited Nov 7 at 11:49
answered Nov 7 at 11:30
Geckon01
213
213
still nothing, i've copied it wrong. The actual file is correct but it still doesn't work
– Fabzheimer
Nov 7 at 11:36
You want to toggle required on first field?
– Geckon01
Nov 7 at 11:47
Also, I've added some info to my answer. Read this please
– Geckon01
Nov 7 at 11:53
add a comment |
still nothing, i've copied it wrong. The actual file is correct but it still doesn't work
– Fabzheimer
Nov 7 at 11:36
You want to toggle required on first field?
– Geckon01
Nov 7 at 11:47
Also, I've added some info to my answer. Read this please
– Geckon01
Nov 7 at 11:53
still nothing, i've copied it wrong. The actual file is correct but it still doesn't work
– Fabzheimer
Nov 7 at 11:36
still nothing, i've copied it wrong. The actual file is correct but it still doesn't work
– Fabzheimer
Nov 7 at 11:36
You want to toggle required on first field?
– Geckon01
Nov 7 at 11:47
You want to toggle required on first field?
– Geckon01
Nov 7 at 11:47
Also, I've added some info to my answer. Read this please
– Geckon01
Nov 7 at 11:53
Also, I've added some info to my answer. Read this please
– Geckon01
Nov 7 at 11:53
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53188394%2ftoggle-required-field-via-checkbox-javascript%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
First of all, there is an issue here : var chboxs = document.getElementsByName("more"); There is ID with this name..
– Alon Shmiel
Nov 7 at 11:25