I am trying to create a radio button that opens up an additional checkbox. When I run the code, the checkbox is already open. what can I do to fix it?
<script>
function accountFunction() {
if (document.getElementById('yesSrm').checked) {
document.getElementById('acct').style.display = "inline";
} else {
document.getElementById('acct').style.display = "none";
}
</script>
<form>
<input id="yesSrm" name="yesSrm" type="radio" value="yes" onchange="accountFunction()" />SRM<br/>
<br/>
<div id="acct">
<label for="account">Account</label>
<input type="checkbox" name="account" id="account"><br/>
</div>
</form>
How it appears
Just add display:none to acct block to hide it by default
<script>
function accountFunction() {
if (document.getElementById('yesSrm').checked) {
document.getElementById('acct').style.display = "block";
} else {
document.getElementById('acct').style.display = "none";
}
}
</script>
<form>
<input id="yesSrm" name="yesSrm" type="radio" value="yes" onchange="accountFunction()" />SRM<br/>
<br/>
<div id="acct" style="display: none">
<label for="account">Account</label>
<input type="checkbox" name="account" id="account"><br/>
</div>
</form>
Related
I am having some trouble with my checkboxes. I am trying to get a returned true or false value out of event listeners on checkboxes and then passing it through an event listener on a button to confirm that at least one checkbox is selected. I'm sorry if this sounds confusing and I feel like there is an easier way to do this. I would like to solve this with pure JavaScript. Really appreciate the help. Below is the code.
<body>
<script src="racquetObjects.js"></script>
<div class="mainContainer">
<div class="selectors">
<form action="">
<div class="checkboxes">
<input type="checkbox" id="babolat" value="Babolat" />
<label for="babolat">Babolat</label>
<input type="checkbox" id="wilson" value="Wilson" />
<label for="wilson">Wilson</label>
<input type="checkbox" id="power" value="Power" />
<label for="power">Power</label>
<input type="checkbox" id="control" value="Control" />
<label for="control">Control</label>
<input type="checkbox" id="popular" value="Popular" />
<label for="popular">Popular</label>
</div>
<button type="button" id="find">Find</button>
</form>
</div>
<div class="racquetContainer"></div>
<div class="bench"></div>
</div>
<script src="racquetFinderCB.js"></script>
<script src="racquetFinder.js"></script>
</body>
const findButton = document.querySelector("#find");
const checkboxes = document.querySelectorAll(
".checkboxes input[type=checkbox]"
);
const checkboxesAreChecked = checkboxes.forEach((el) => {
el.addEventListener("click", (e) => {
if (e.target.checked) {
return true;
} else {
return false;
}
});
});
const beforeSubmit = () => {
if (checkboxesAreChecked === true) {
console.log("Time to search!");
} else {`enter code here`
console.log("You need to select an option");
}
};
In this example there is an event listener for the form submit. An array of the input elements is filtered, so only the checked will end up in checked.
The first e.preventDefault() is just for testing. If the form should submit (something was checked) then remove that line of code.
document.forms.find.addEventListener('submit', e => {
let inputs = e.target.querySelectorAll('input');
e.preventDefault(); // prevent default to test what happens
let checked = [...inputs].filter(input => input.checked);
if (checked.length > 0) {
console.log('at least one was checked');
} else {
e.preventDefault(); // prevent default to stop the form action
console.log('none was checked');
}
});
<div class="mainContainer">
<div class="selectors">
<form name="find" action="">
<div class="checkboxes">
<input type="checkbox" id="babolat" value="Babolat" />
<label for="babolat">Babolat</label>
<input type="checkbox" id="wilson" value="Wilson" />
<label for="wilson">Wilson</label>
<input type="checkbox" id="power" value="Power" />
<label for="power">Power</label>
<input type="checkbox" id="control" value="Control" />
<label for="control">Control</label>
<input type="checkbox" id="popular" value="Popular" />
<label for="popular">Popular</label>
</div>
<button id="find">Find</button>
</form>
</div>
<div class="racquetContainer"></div>
<div class="bench"></div>
</div>
I want to use 3 input checkbox to display 3 different div. I am using this code but the only one working is "Course 1" and I can't figure out why. I guess it is something pretty easy, but I can't see it:
document.getElementById('checkbox1');
checkbox1.onchange = function() {
if (checkbox1.checked) {
course1.style.display = 'block';
} else {
course1.style.display = 'none';
}
};
document.getElementById('checkbox2');
checkbox2.onchange = function() {
if (checkbox2.checked) {
course2.style.display = 'block';
} else {
course2.style.display = 'none';
}
};
document.getElementById('checkbox3');
checkbox3.onchange = function() {
if (checkbox3.checked) {
course3.style.display = 'block';
} else {
course3.style.display = 'none';
}
};
<form>
<label class="switch">
<input type="checkbox" id="checkbox1" checked="true"> Course 1
</label>
</form>
<form>
<label class="switch">
<input type="checkbox" id="checkbox2" checked="true"> Course 2
</label>
</form>
<form>
<label class="switch">
<input type="checkbox" id="checkbox3" checked="true"> Course 3
</label>
</form>
<br>
<div id="course1"> Text course 1
</div>
<br>
<div id="course2"> Text course 2
</div>
<br>
<div id="course2"> Text course 3
</div>
Example: https://codepen.io/antonioagar1/pen/dqGaoO
You can try this simple code instead of your own code:
document.addEventListener("change", function(ev){
if(ev.target.id.substr(0,8)=="checkbox") document.querySelector("[id='course"+ev.target.getAttribute("id").slice(-1)+"']").style.display=ev.target.checked?"block":"none";
});
you have two div with id course2. first, change it to course3 and then try.
Check result online
I have this sample:
link
CODE HTML:
<div class="box1">
<fieldset>
<input type="checkbox" class="select-all">
<label>Select All </label>
</fieldset>
<fieldset>
<input type="checkbox">
<label>Checkbox1</label>
</fieldset>
<fieldset>
<input type="checkbox">
<label>Checkbox3 </label>
</fieldset>
<fieldset>
<input type="checkbox">
<label>Checkbox3 </label>
</fieldset>
</div>
<div class="box2">
<fieldset>
<input type="checkbox" class="select-all">
<label>Select All </label>
</fieldset>
<fieldset>
<input type="checkbox">
<label>Checkbox4 </label>
</fieldset>
<fieldset>
<input type="checkbox">
<label>Checkbox5 </label>
</fieldset>
<fieldset>
<input type="checkbox">
<label>Checkbox6 </label>
</fieldset>
</div>
CODE CSS:
.box1{
background:#d9d9d9;
margin-bottom:20px;
}
.box2{
background:#ccc;
}
CODE JS:
$('.select-all').click(function(event) {
if(this.checked) {
// Iterate each checkbox
$(':checkbox').each(function() {
this.checked = true;
});
}else{
$(':checkbox').each(function() {
this.checked = false;
});
}
});
What I want to do is that when the button "select-all" is checked, select all fields in a box.
At this time, set all inputs ... and I wish only the current box.
How can I change my function so that it works?
Thanks in advance!
You can use .closest() to find the parent div, then use .find() method to get the checkboxes inside it,
$('.select-all').click(function(event) {
if (this.checked) {
// Iterate each checkbox
$(this).closest("div").find(':checkbox').each(function() {
this.checked = true;
});
} else {
$(this).closest("div").find(':checkbox').each(function() {
this.checked = false;
});
}
});
Demo
You can reduce your code like this also,
$('.select-all').click(function(event) {
var obj=this;
var parent = $(this).closest("div[class^=box]");
parent.find(':checkbox').each(function() {
this.checked = obj.checked;
});
});
As A.Wolf suggested, you can use like this too,
$('.select-all').change(function(event) {
$(this).closest('div[class^=box]').find(':checkbox').prop('checked', this.checked);
});
div1 = Top 10 Listings
div2 = Send Selected
am looking for functionality that will be having n no. of contacts each with a checkbox when checkbox checked div2 has to be displayed otherwise div1 should have to be displayed... Am entirely new to this forum sry if my qtn is confusing below is what I've achieved upto now... http://jsfiddle.net/0p5cf4be/
now checkbox is working as a toggle
but I need that div2 to display if one/multiple checkbox checked it should stay at div2
at default it should have to show div1
if no checkbox is checked then it have to show div1
<script>
x=false;
function Check(){
if(x){
document.getElementById("div1").style.display='inline';
document.getElementById("div2").style.display='none';
x=false;
}
else{
document.getElementById("div1").style.display='none';
document.getElementById("div2").style.display='inline';
x=true;
}
}
</script>
<div>
<h2>Top 10 Listings</h2>
<h2>Send Selected</h2>
</div>
<div class="contacts">
Contact 1<input type="checkbox" id="check" onclick="Check()">
<br>
Contact 2<input type="checkbox" id="check" onclick="Check()">
<br>
Contact 3<input type="checkbox" id="check" onclick="Check()">
</div>
What about using jquery to manage the state of div2 based on the state of the checkboxes:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<div>
<a href="#" id="div1">
<h2>Top 10 Listings</h2> </a>
<a href="#" id="div2" style="display: none;">
<h2>Send Selected</h2> </a>
</div>
<div class="contacts">
Contact 1<input type="checkbox" id="check1">
<br>
Contact 2<input type="checkbox" id="check2">
<br>
Contact 3<input type="checkbox" id="check2">
</div>
<script>
var allCheckboxes = $("input[type=checkbox]");
allCheckboxes.click(
function () {
var showSendSelected = $("input[type=checkbox]:checked").length > 0;
var sendSelectedLink = $("#div2");
if (showSendSelected) {
sendSelectedLink.show();
} else {
sendSelectedLink.hide();
}
}
);
</script>
Since you have used jQuery tag, use a jQuery handler like
jQuery(function($) {
var $checks = $('.contacts input:checkbox').click(function() {
var checked = $checks.is(':checked');
$('#div1').toggle(!checked);
$('#div2').toggle(checked);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div>
<h2>Top 10 Listings</h2>
<h2>Send Selected</h2>
</div>
<div class="contacts">
Contact 1<input type="checkbox"/>
<br/>
Contact 2<input type="checkbox"/>
<br/>
Contact 3<input type="checkbox"/>
</div>
Without jQuery, you can use a counter like
var counter = 0;
function Check(el) {
if (el.checked) {
counter++;
} else {
counter--;
}
document.getElementById("div1").style.display = counter ? 'none' : 'block';
document.getElementById("div2").style.display = counter ? 'block' : 'none';
}
<div>
<h2>Top 10 Listings</h2>
<h2>Send Selected</h2>
</div>
<div class="contacts">
Contact 1<input type="checkbox" onclick="Check(this)"/>
<br/>
Contact 2<input type="checkbox" onclick="Check(this)"/>
<br/>
Contact 3<input type="checkbox" onclick="Check(this)"/>
</div>
Since you have tagged JQUERY try this,
function Check()
{
var y=$("input[type='checkbox']:checked").length;
if(y!=0)
{
document.getElementById("div2").style.display='inline';
document.getElementById("div1").style.display='none';
}
else
{
document.getElementById("div1").style.display='inline';
document.getElementById("div2").style.display='none';
}
}
Fiddle link : http://jsfiddle.net/0p5cf4be/11/
I have 2 div tags in my HTML code and I have defined a class by which I can expand or hide them.The problem that I have is the fact that when I click on one of the the value of the other changes.
<HTML>
<BODY>
<FORM>
<script language="JavaScript">
function setVisibility(id) {
if (document.getElementById('btnshowhide').value == '-') {
document.getElementById('btnshowhide').value = '+';
document.getElementById(id).style.display = 'none';
} else {
document.getElementById('btnshowhide').value = '-';
document.getElementById(id).style.display = 'inline';
}
}
</script>
<FIELDSET style="width: 600px;">
<LEGEND>
<input type=button name=type id='btnshowhide' value='+' onclick="setVisibility('div1');" ;/>Insert Data:</LEGEND>
<DIV id="div1" style="display: none;">
<LABEL for="351fef76-b826-426f-88c4-dbaaa60f886b">Name:</LABEL>
<TEXTAREA id="351fef76-b826 -426f-88c4-dbaaa60f886b" name="txtname" value="Name" type="text" title="Name:">Name</TEXTAREA>
<BR>
<LABEL for="02973dcc-5677-417c-a9bf-1578f58923ef">Family:</LABEL>
<TEXTAREA id="02973dcc-5677-417c-a9bf-1578f58923ef" name="txtFamiy" value="Family" type="text" title="Family:">Family</TEXTAREA>
<BR>
</DIV>
</FIELDSET>
<BR>
<FIELDSET style="width: 600px;">
<LEGEND>
<input type=button name=type id='btnshowhide' value='+' onclick="setVisibility('div2');" ;/>Insert Data:</LEGEND>
<DIV id="div2" style="display: none;">
<LABEL for="d8876943-5861-4d62-9249-c5fef88219fa">Type of property</LABEL>
<SELECT id="d8876943-5861-4d62-9249-c5fef88219fa" name="PropertyType" value="" type="select" title="Type of property"></SELECT>
<BR>
</DIV>
</FIELDSET>
<BR>
</FORM>
</BODY>
</HTML>
I tried changing the Ids of my buttons but nothing changed.
change you buttons to --
<input type="button" name="type" id="btnshowhide1" value="+" onclick="setVisibility('div1',this.id);";/>
and
<input type="button" name="type" id="btnshowhide2" value="+" onclick="setVisibility('div2',this.id);";/>
And use following function --
function setVisibility(id,buttonid) {
if(document.getElementById(buttonid).value=='-'){
document.getElementById(buttonid).value = '+';
document.getElementById(id).style.display = 'none';
}
else{
document.getElementById(buttonid).value = '-';
document.getElementById(id).style.display = 'inline';
}
}
DEMO
First of all, you shouldn`t have two elements with same ID. You should use class for it.
And it happens because you have both buttons with id="btnshowhide" so when you are trying to do document.getElementById('btnshowhide').value it matches you both buttons.
you can do something like this
<script language="JavaScript">
function setVisibility(id,input)
{
if(input.value=='-')
{
input.value = '+';
document.getElementById(id).style.display = 'none';
}
else
{
input.value = '-';
document.getElementById(id).style.display = 'inline';
}
}
</script>
and in your inputs
onclick="setVisibility('div1', this);"
onclick="setVisibility('div2', this);"