How to add custom checkbox with event in customer registration Magento 2?












2
















I'm working on creating a custom checkbox with an event




<div class="field tcagreecreateaccount required">
<div class="control">
<input type="checkbox" id="tcagreecreateaccount" name="tcagreecreateaccount" data-validate="{required:false}" class="input-checkbox checkbox required" value="1">
<label for="tcagreecreateaccount" class="label">
<?= __('Custom checkbox') ?>
</label>
</div>
</div>



1)when the checkbox is ticked need to show
extra two fields in customer registration



2)How I change the checkbox as optional one




How I can do it ???

Any ideas?



Edit 1:



addtional.phtml



<div id="idofyourinputwrapper">
<div class="field skype required">
<label class="label" for="skype2">
<span><?= $block->escapeHtml(__('Check now')) ?></span>
</label>
<div class="control">
<input type="text" name="skype2" id="skype22" value="" title="<?= $block->escapeHtmlAttr(__('Check now')) ?>" class="input-text" data-validate="{required:false}">
</div>
</div>

</div>









share|improve this question

























  • hm wait..........

    – prabhakaran7
    Mar 16 at 7:42
















2
















I'm working on creating a custom checkbox with an event




<div class="field tcagreecreateaccount required">
<div class="control">
<input type="checkbox" id="tcagreecreateaccount" name="tcagreecreateaccount" data-validate="{required:false}" class="input-checkbox checkbox required" value="1">
<label for="tcagreecreateaccount" class="label">
<?= __('Custom checkbox') ?>
</label>
</div>
</div>



1)when the checkbox is ticked need to show
extra two fields in customer registration



2)How I change the checkbox as optional one




How I can do it ???

Any ideas?



Edit 1:



addtional.phtml



<div id="idofyourinputwrapper">
<div class="field skype required">
<label class="label" for="skype2">
<span><?= $block->escapeHtml(__('Check now')) ?></span>
</label>
<div class="control">
<input type="text" name="skype2" id="skype22" value="" title="<?= $block->escapeHtmlAttr(__('Check now')) ?>" class="input-text" data-validate="{required:false}">
</div>
</div>

</div>









share|improve this question

























  • hm wait..........

    – prabhakaran7
    Mar 16 at 7:42














2












2








2









I'm working on creating a custom checkbox with an event




<div class="field tcagreecreateaccount required">
<div class="control">
<input type="checkbox" id="tcagreecreateaccount" name="tcagreecreateaccount" data-validate="{required:false}" class="input-checkbox checkbox required" value="1">
<label for="tcagreecreateaccount" class="label">
<?= __('Custom checkbox') ?>
</label>
</div>
</div>



1)when the checkbox is ticked need to show
extra two fields in customer registration



2)How I change the checkbox as optional one




How I can do it ???

Any ideas?



Edit 1:



addtional.phtml



<div id="idofyourinputwrapper">
<div class="field skype required">
<label class="label" for="skype2">
<span><?= $block->escapeHtml(__('Check now')) ?></span>
</label>
<div class="control">
<input type="text" name="skype2" id="skype22" value="" title="<?= $block->escapeHtmlAttr(__('Check now')) ?>" class="input-text" data-validate="{required:false}">
</div>
</div>

</div>









share|improve this question

















I'm working on creating a custom checkbox with an event




<div class="field tcagreecreateaccount required">
<div class="control">
<input type="checkbox" id="tcagreecreateaccount" name="tcagreecreateaccount" data-validate="{required:false}" class="input-checkbox checkbox required" value="1">
<label for="tcagreecreateaccount" class="label">
<?= __('Custom checkbox') ?>
</label>
</div>
</div>



1)when the checkbox is ticked need to show
extra two fields in customer registration



2)How I change the checkbox as optional one




How I can do it ???

Any ideas?



Edit 1:



addtional.phtml



<div id="idofyourinputwrapper">
<div class="field skype required">
<label class="label" for="skype2">
<span><?= $block->escapeHtml(__('Check now')) ?></span>
</label>
<div class="control">
<input type="text" name="skype2" id="skype22" value="" title="<?= $block->escapeHtmlAttr(__('Check now')) ?>" class="input-text" data-validate="{required:false}">
</div>
</div>

</div>






magento2 javascript custom-field checkbox






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 16 at 8:29







prabhakaran7

















asked Mar 16 at 6:12









prabhakaran7prabhakaran7

1969




1969













  • hm wait..........

    – prabhakaran7
    Mar 16 at 7:42



















  • hm wait..........

    – prabhakaran7
    Mar 16 at 7:42

















hm wait..........

– prabhakaran7
Mar 16 at 7:42





hm wait..........

– prabhakaran7
Mar 16 at 7:42










2 Answers
2






active

oldest

votes


















1














Try this,



Replace this



<div class="field tcagreecreateaccount required">
<div class="control">
<input type="checkbox" id="tcagreecreateaccount" name="tcagreecreateaccount" data-validate="{required:false}" class="input-checkbox checkbox required" value="1">
<label for="tcagreecreateaccount" class="label">
<?= __('Custom checkbox') ?>
</label>
</div>
</div>


with this



<div class="field tcagreecreateaccount">
<div class="control">
<input type="checkbox" id="tcagreecreateaccount" name="tcagreecreateaccount" class="input-checkbox checkbox" value="1">
<label for="tcagreecreateaccount" class="label">
<?= __('Custom checkbox') ?>
</label>
</div>
</div>


then add the below script in your phtml



require(['jquery'],function($) {
$(document).ready(function () {
var ckbox = $('#tcagreecreateaccount');
$('#tcagreecreateaccount').on('click',function () {
if (ckbox.is(':checked')) {
$('#idofyourinputwrapper').show(); //idofyourinputwrapper is your input wrapper
} else {
$('#idofyourinputwrapper').hide();
}
});
});
});


wrap your two inputs with a div like below.



<div id="idofyourinputwrapper">


Hope this helps :)






share|improve this answer


























  • i tried but not event working....

    – prabhakaran7
    Mar 16 at 7:57











  • Event?? what that mean? The above code do, remove required field and when you click checkbox it will show and hide the below field. Please update your code

    – Prathap Gunasekaran
    Mar 16 at 8:00











  • i added this not working <div id="idofyourinputwrapper"> <div class="field skype required"> <label class="label" for="skype2"> <span><?= $block->escapeHtml(__('Check now')) ?></span> </label> <div class="control"> <input type="text" name="skype2" id="skype22" value="" title="<?= $block->escapeHtmlAttr(__('Check now')) ?>" class="input-text" data-validate="{required:false}"> </div> </div> </div>

    – prabhakaran7
    Mar 16 at 8:05











  • i set is required:false

    – prabhakaran7
    Mar 16 at 8:06











  • Have you added script ??

    – Prathap Gunasekaran
    Mar 16 at 8:07



















0














Firstly remove required from your checkbox and other two attributes.



Both attributes depending on checkbox click will be hidden by default (can do with CSS or JS).



On checkbox click, you can use jQuery to display both attributes.






share|improve this answer
























  • done.........................

    – prabhakaran7
    Mar 16 at 10:41











Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "479"
};
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',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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%2fmagento.stackexchange.com%2fquestions%2f266159%2fhow-to-add-custom-checkbox-with-event-in-customer-registration-magento-2%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Try this,



Replace this



<div class="field tcagreecreateaccount required">
<div class="control">
<input type="checkbox" id="tcagreecreateaccount" name="tcagreecreateaccount" data-validate="{required:false}" class="input-checkbox checkbox required" value="1">
<label for="tcagreecreateaccount" class="label">
<?= __('Custom checkbox') ?>
</label>
</div>
</div>


with this



<div class="field tcagreecreateaccount">
<div class="control">
<input type="checkbox" id="tcagreecreateaccount" name="tcagreecreateaccount" class="input-checkbox checkbox" value="1">
<label for="tcagreecreateaccount" class="label">
<?= __('Custom checkbox') ?>
</label>
</div>
</div>


then add the below script in your phtml



require(['jquery'],function($) {
$(document).ready(function () {
var ckbox = $('#tcagreecreateaccount');
$('#tcagreecreateaccount').on('click',function () {
if (ckbox.is(':checked')) {
$('#idofyourinputwrapper').show(); //idofyourinputwrapper is your input wrapper
} else {
$('#idofyourinputwrapper').hide();
}
});
});
});


wrap your two inputs with a div like below.



<div id="idofyourinputwrapper">


Hope this helps :)






share|improve this answer


























  • i tried but not event working....

    – prabhakaran7
    Mar 16 at 7:57











  • Event?? what that mean? The above code do, remove required field and when you click checkbox it will show and hide the below field. Please update your code

    – Prathap Gunasekaran
    Mar 16 at 8:00











  • i added this not working <div id="idofyourinputwrapper"> <div class="field skype required"> <label class="label" for="skype2"> <span><?= $block->escapeHtml(__('Check now')) ?></span> </label> <div class="control"> <input type="text" name="skype2" id="skype22" value="" title="<?= $block->escapeHtmlAttr(__('Check now')) ?>" class="input-text" data-validate="{required:false}"> </div> </div> </div>

    – prabhakaran7
    Mar 16 at 8:05











  • i set is required:false

    – prabhakaran7
    Mar 16 at 8:06











  • Have you added script ??

    – Prathap Gunasekaran
    Mar 16 at 8:07
















1














Try this,



Replace this



<div class="field tcagreecreateaccount required">
<div class="control">
<input type="checkbox" id="tcagreecreateaccount" name="tcagreecreateaccount" data-validate="{required:false}" class="input-checkbox checkbox required" value="1">
<label for="tcagreecreateaccount" class="label">
<?= __('Custom checkbox') ?>
</label>
</div>
</div>


with this



<div class="field tcagreecreateaccount">
<div class="control">
<input type="checkbox" id="tcagreecreateaccount" name="tcagreecreateaccount" class="input-checkbox checkbox" value="1">
<label for="tcagreecreateaccount" class="label">
<?= __('Custom checkbox') ?>
</label>
</div>
</div>


then add the below script in your phtml



require(['jquery'],function($) {
$(document).ready(function () {
var ckbox = $('#tcagreecreateaccount');
$('#tcagreecreateaccount').on('click',function () {
if (ckbox.is(':checked')) {
$('#idofyourinputwrapper').show(); //idofyourinputwrapper is your input wrapper
} else {
$('#idofyourinputwrapper').hide();
}
});
});
});


wrap your two inputs with a div like below.



<div id="idofyourinputwrapper">


Hope this helps :)






share|improve this answer


























  • i tried but not event working....

    – prabhakaran7
    Mar 16 at 7:57











  • Event?? what that mean? The above code do, remove required field and when you click checkbox it will show and hide the below field. Please update your code

    – Prathap Gunasekaran
    Mar 16 at 8:00











  • i added this not working <div id="idofyourinputwrapper"> <div class="field skype required"> <label class="label" for="skype2"> <span><?= $block->escapeHtml(__('Check now')) ?></span> </label> <div class="control"> <input type="text" name="skype2" id="skype22" value="" title="<?= $block->escapeHtmlAttr(__('Check now')) ?>" class="input-text" data-validate="{required:false}"> </div> </div> </div>

    – prabhakaran7
    Mar 16 at 8:05











  • i set is required:false

    – prabhakaran7
    Mar 16 at 8:06











  • Have you added script ??

    – Prathap Gunasekaran
    Mar 16 at 8:07














1












1








1







Try this,



Replace this



<div class="field tcagreecreateaccount required">
<div class="control">
<input type="checkbox" id="tcagreecreateaccount" name="tcagreecreateaccount" data-validate="{required:false}" class="input-checkbox checkbox required" value="1">
<label for="tcagreecreateaccount" class="label">
<?= __('Custom checkbox') ?>
</label>
</div>
</div>


with this



<div class="field tcagreecreateaccount">
<div class="control">
<input type="checkbox" id="tcagreecreateaccount" name="tcagreecreateaccount" class="input-checkbox checkbox" value="1">
<label for="tcagreecreateaccount" class="label">
<?= __('Custom checkbox') ?>
</label>
</div>
</div>


then add the below script in your phtml



require(['jquery'],function($) {
$(document).ready(function () {
var ckbox = $('#tcagreecreateaccount');
$('#tcagreecreateaccount').on('click',function () {
if (ckbox.is(':checked')) {
$('#idofyourinputwrapper').show(); //idofyourinputwrapper is your input wrapper
} else {
$('#idofyourinputwrapper').hide();
}
});
});
});


wrap your two inputs with a div like below.



<div id="idofyourinputwrapper">


Hope this helps :)






share|improve this answer















Try this,



Replace this



<div class="field tcagreecreateaccount required">
<div class="control">
<input type="checkbox" id="tcagreecreateaccount" name="tcagreecreateaccount" data-validate="{required:false}" class="input-checkbox checkbox required" value="1">
<label for="tcagreecreateaccount" class="label">
<?= __('Custom checkbox') ?>
</label>
</div>
</div>


with this



<div class="field tcagreecreateaccount">
<div class="control">
<input type="checkbox" id="tcagreecreateaccount" name="tcagreecreateaccount" class="input-checkbox checkbox" value="1">
<label for="tcagreecreateaccount" class="label">
<?= __('Custom checkbox') ?>
</label>
</div>
</div>


then add the below script in your phtml



require(['jquery'],function($) {
$(document).ready(function () {
var ckbox = $('#tcagreecreateaccount');
$('#tcagreecreateaccount').on('click',function () {
if (ckbox.is(':checked')) {
$('#idofyourinputwrapper').show(); //idofyourinputwrapper is your input wrapper
} else {
$('#idofyourinputwrapper').hide();
}
});
});
});


wrap your two inputs with a div like below.



<div id="idofyourinputwrapper">


Hope this helps :)







share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 16 at 10:01

























answered Mar 16 at 7:29









Prathap GunasekaranPrathap Gunasekaran

1,4071617




1,4071617













  • i tried but not event working....

    – prabhakaran7
    Mar 16 at 7:57











  • Event?? what that mean? The above code do, remove required field and when you click checkbox it will show and hide the below field. Please update your code

    – Prathap Gunasekaran
    Mar 16 at 8:00











  • i added this not working <div id="idofyourinputwrapper"> <div class="field skype required"> <label class="label" for="skype2"> <span><?= $block->escapeHtml(__('Check now')) ?></span> </label> <div class="control"> <input type="text" name="skype2" id="skype22" value="" title="<?= $block->escapeHtmlAttr(__('Check now')) ?>" class="input-text" data-validate="{required:false}"> </div> </div> </div>

    – prabhakaran7
    Mar 16 at 8:05











  • i set is required:false

    – prabhakaran7
    Mar 16 at 8:06











  • Have you added script ??

    – Prathap Gunasekaran
    Mar 16 at 8:07



















  • i tried but not event working....

    – prabhakaran7
    Mar 16 at 7:57











  • Event?? what that mean? The above code do, remove required field and when you click checkbox it will show and hide the below field. Please update your code

    – Prathap Gunasekaran
    Mar 16 at 8:00











  • i added this not working <div id="idofyourinputwrapper"> <div class="field skype required"> <label class="label" for="skype2"> <span><?= $block->escapeHtml(__('Check now')) ?></span> </label> <div class="control"> <input type="text" name="skype2" id="skype22" value="" title="<?= $block->escapeHtmlAttr(__('Check now')) ?>" class="input-text" data-validate="{required:false}"> </div> </div> </div>

    – prabhakaran7
    Mar 16 at 8:05











  • i set is required:false

    – prabhakaran7
    Mar 16 at 8:06











  • Have you added script ??

    – Prathap Gunasekaran
    Mar 16 at 8:07

















i tried but not event working....

– prabhakaran7
Mar 16 at 7:57





i tried but not event working....

– prabhakaran7
Mar 16 at 7:57













Event?? what that mean? The above code do, remove required field and when you click checkbox it will show and hide the below field. Please update your code

– Prathap Gunasekaran
Mar 16 at 8:00





Event?? what that mean? The above code do, remove required field and when you click checkbox it will show and hide the below field. Please update your code

– Prathap Gunasekaran
Mar 16 at 8:00













i added this not working <div id="idofyourinputwrapper"> <div class="field skype required"> <label class="label" for="skype2"> <span><?= $block->escapeHtml(__('Check now')) ?></span> </label> <div class="control"> <input type="text" name="skype2" id="skype22" value="" title="<?= $block->escapeHtmlAttr(__('Check now')) ?>" class="input-text" data-validate="{required:false}"> </div> </div> </div>

– prabhakaran7
Mar 16 at 8:05





i added this not working <div id="idofyourinputwrapper"> <div class="field skype required"> <label class="label" for="skype2"> <span><?= $block->escapeHtml(__('Check now')) ?></span> </label> <div class="control"> <input type="text" name="skype2" id="skype22" value="" title="<?= $block->escapeHtmlAttr(__('Check now')) ?>" class="input-text" data-validate="{required:false}"> </div> </div> </div>

– prabhakaran7
Mar 16 at 8:05













i set is required:false

– prabhakaran7
Mar 16 at 8:06





i set is required:false

– prabhakaran7
Mar 16 at 8:06













Have you added script ??

– Prathap Gunasekaran
Mar 16 at 8:07





Have you added script ??

– Prathap Gunasekaran
Mar 16 at 8:07













0














Firstly remove required from your checkbox and other two attributes.



Both attributes depending on checkbox click will be hidden by default (can do with CSS or JS).



On checkbox click, you can use jQuery to display both attributes.






share|improve this answer
























  • done.........................

    – prabhakaran7
    Mar 16 at 10:41
















0














Firstly remove required from your checkbox and other two attributes.



Both attributes depending on checkbox click will be hidden by default (can do with CSS or JS).



On checkbox click, you can use jQuery to display both attributes.






share|improve this answer
























  • done.........................

    – prabhakaran7
    Mar 16 at 10:41














0












0








0







Firstly remove required from your checkbox and other two attributes.



Both attributes depending on checkbox click will be hidden by default (can do with CSS or JS).



On checkbox click, you can use jQuery to display both attributes.






share|improve this answer













Firstly remove required from your checkbox and other two attributes.



Both attributes depending on checkbox click will be hidden by default (can do with CSS or JS).



On checkbox click, you can use jQuery to display both attributes.







share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 16 at 6:47









Amrit Pal SinghAmrit Pal Singh

791523




791523













  • done.........................

    – prabhakaran7
    Mar 16 at 10:41



















  • done.........................

    – prabhakaran7
    Mar 16 at 10:41

















done.........................

– prabhakaran7
Mar 16 at 10:41





done.........................

– prabhakaran7
Mar 16 at 10:41


















draft saved

draft discarded




















































Thanks for contributing an answer to Magento Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f266159%2fhow-to-add-custom-checkbox-with-event-in-customer-registration-magento-2%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







Popular posts from this blog

Category:香港粉麵

List *all* the tuples!

Channel [V]