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?










share|improve this question
























  • 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















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?










share|improve this question
























  • 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













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?










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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


















  • 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












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>








share|improve this answer























  • 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











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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

























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>








share|improve this answer























  • 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















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>








share|improve this answer























  • 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













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>








share|improve this answer














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>






share|improve this answer














share|improve this answer



share|improve this answer








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


















  • 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


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini