This question already has answers here:
Check/Uncheck checkbox with JavaScript
(12 answers)
Closed 7 months ago.
I have parent element like:
<span style="background: yellow; padding: 50px;" onClick="setCheckBox()"><span>
<span> </span> <input type="checkbox" name="f01" value="100"></span></span>
and js function:
function setCheckBox() {
document.getElementsByTagName('input'[0].checked = 'true';
}
Want to change the checkbox input on parent click (yellow color)
Thanks in advance!
you would use .checked = true; like so
document.getElementsByTagName('input')[0].checked = true;
<input type="checkbox" name="f01" value="100">
Related
This question already has answers here:
How to handle change of checkbox using jQuery?
(7 answers)
How to listen to when a checkbox is checked in Jquery
(7 answers)
Closed 2 years ago.
Is there a reason why I don't see an alert when the checkbox is checked in my code?
jQuery(function($) {
if ($('input#choice_3_37_1').is(':checked')) {
alert('test');
$("#label_3_37_1").addClass("gquiz-correct-choice");
}
});
.gquiz-correct-choice {
background: green;
color: white
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="checkbox" id="choice_3_37_1" name="change">
<hr>
<label id="label_3_37_1">this should change to green!</label>
This question already has answers here:
How can I check if a checkbox is checked?
(18 answers)
Closed 2 years ago.
I want to implement a dark mode in my website. I have a switch with a checkbox input and I want to run a javascript function when it is checked so that the colors change. Here is the html code. I don't know if the CSS code is needed for this.
<label class="switch">
<input type="checkbox" id="darkmode">
<span class="slider round"></span>
</label>
var checkbox = document.getElementById("darkmode");
checkbox.addEventListener( 'change', function() {
if(this.checked) {
// Enable dark mode
} else {
// Disable dark mode
}
});
function check() {
var check= document.getElementById("darkmode").checked;
console.log(check);
}
<label class="switch">
<input type="checkbox" id="darkmode" >
<span class="slider round"></span>
<button onclick='check()'>press</button>
</label>
This question already has answers here:
Can I make a <button> not submit a form?
(8 answers)
Closed 6 years ago.
I try to make a calculator with javascript with the onclick event I want the value of the button to convert an input text box.
When I write the code the value of the button briefly in the text box , but it disappears instantly.
The code that i use can you find below this text, what am i doing wrong.
HTML:
var iGetal = 0;
function GetalRekenmachine()
{
iGetal = iGetal + 1;
document.getElementById("Display").value = iGetal;
}
<button id="Cijfer7" value="7" onclick="GetalRekenmachine()">7</button>
<input type="text" id="Display"/>
Because the default type of a button is submit. So either cancel the click action or set the type to be button.
<button id="Cijfer7" value="7" onclick="GetalRekenmachine(); return false;">7</button>
or
<button type="button" id="Cijfer7" value="7" onclick="GetalRekenmachine()">7</button>
var iGetal = 0;
function GetalRekenmachine(event)
{
iGetal = iGetal + 1;
document.getElementById("Display").value = iGetal;
event.preventDefault()
}
<button id="Cijfer7" value="7" onclick="GetalRekenmachine()">7</button>
<input type="text" id="Display"/>
This question already has answers here:
How to get the selected radio button’s value?
(19 answers)
Closed 8 years ago.
undefined output!! am i missing sth ? any idea ?
here is the radio button inputs :
<tr>
<td>Yes <input type="radio" name="email" id="email" value="yes"/></td>
<td></td>
<td>No <input type="radio" name="email" id="email" value="no"checked="checked"/></td>
</tr>
<input type="button" value="submit" onclick="doIt()" style="width: 150px; height:30px" />
<script type="text/javascript" language="javascript">
function doIt() {
var emailopt = document.getElementById('email').checked.value;
alert (emailopt);
}
The way you have tried is completely wrong. Instead use the below approach
1) Iterate through all the radio buttons with name property
2) Now using checked attribute, place an if statement and then extract the value
function doIt() {
var emailopt = document.getElementsByName('email');
for (i = 0; i < emailopt.length; i++) {
if (emailopt[i].checked) {
alert(emailopt[i].value)
}
}
}
Fiddle
Try using this:
function doIt() {
var emailopt = document.getElementById('email').checked;
alert (emailopt);
}
It will return true and false.
Lets me know if you have any questions.I hope it will help you.
This question already has answers here:
Radio Button and an Input field
(3 answers)
Closed 8 years ago.
im trying to activate/disable a textinput by activating/disabling a radio button.
html:
<input name="test" type="radio" value="one" onclick="activate();"/>
<input class="" name="info" type="text" size="5" maxlength="5" disabled>
<input name="test" type="radio" value="two""/>
javascript:
function activate(){
document.forms[0].info.disabled = !document.forms[0].test[0].checked;
}
when u activate the 1st radio button, the input text should be activated. when u activate 2nd button, it should be disabled.
but this code doesnt work. does anyone know better?
You need to run the activate function again when the other radio button is clicked.
Try this:
window.onload=function() {
var rad = document.getElementsByName("test");
for (var i=0;i<rad.length;i++) {
rad[i].onclick=function() {
this.form.info.disabled=this.value!="one";
}
}