<script type="text/javascript">
function ShowHideDiv(chkCat) {
var dvCat = document.getElementById("dvCat");
dvCat.style.display = chkCat.checked ? "block" : "none";
}
</script>
<label for="chkCat">
<input type="checkbox" id="chkCat" onclick="ShowHideDiv(this)" /> Cat
</label>
<div id="dvCat" style="display: none">
<label>
<input type="checkbox" id="dvCat" onclick="ShowHideDiv(this)" /> British shorthair
</label>
<label>
<p id="British shorthair" ></p>
</label>
<script>
document.getElementById("dvCat").addEventListener("click", function(){
document.getElementById("British shorthair").innerHTML = "RM00";
});
</script>
<label>
<input type="checkbox" id="chkCat" onclick="ShowHideDiv(this)" /> Exotic Shorthair
</label>
<p id="Exotic Shorthair"></p>
<script>
document.getElementById("dvCat").addEventListener("click", function(){
document.getElementById("Exotic Shorthair").innerHTML = "RM00";
});
</script>
1 checkbox. Under this another 2 options checkbox. Select one of the two checkbox the output of both these options came out simultaneously. Try to run this coding. Can you fix my coding?
You can fix it for now by assigning unique ID to checkboxes.
<script type="text/javascript">
function ShowHideDiv(chkCat) {
var dvCat = document.getElementById("dvCat");
dvCat.style.display = chkCat.checked ? "block" : "none";
}
</script>
<label for="chkCat">
<input type="checkbox" id="chkCat" onclick="ShowHideDiv(this)" /> Cat
</label>
<div id="dvCat" style="display: none">
<label>
<input type="checkbox" id="dvSubCat" /> British shorthair
</label>
<label>
<p id="British shorthair" ></p>
</label>
<script>
document.getElementById("dvSubCat").addEventListener("click", function(){
document.getElementById("British shorthair").innerHTML = "RM00";
});
</script>
<label>
<input type="checkbox" id="chkSubCat" /> Exotic Shorthair
</label>
<p id="Exotic Shorthair"></p>
<script>
document.getElementById("chkSubCat").addEventListener("click", function(){
document.getElementById("Exotic Shorthair").innerHTML = "RM00";
});
</script>
P.S: Assigning id is not the ultimate solution, if you have more than 1 checkbox falling in same criteria use class names instead
Related
I would like to remove my input value when disabled.
I found some solution here:
Reset disabled input field's value
but it didn't work in my case.
My code looks like this:
<figure class="question">
<label>
<div class="order">5</div>
<p class="question_title">Live stream<span class="asterisk">*</span></p>
</label>
<br>
<input name="live_stream" class="radiobtn" type="radio" value="Yes">Yes
<input name="live_stream" class="radiobtn" type="radio" value="No">No
<br>
</figure>
<script>
$("input[name=live_stream]").on('click', function() {
var autoRefresh = $('#autorefresh');
if ($(this).val() == "No") {
autoRefresh.show();
autoRefresh.find('input,select,radio').prop('disabled', false);
} else {
autoRefresh.find('input,select,radio').prop('disabled', true);
autoRefresh.find('input,select,radio').val()= null ;
}
});
</script>
<figure class="question" id="autorefresh">
<label>
<div class="order">6</div>
<p class="question_title">Autorefresh frequency (in seconds)<span class="asterisk">*</span></p>
</label>
<input type="number" id="autoref" name="autorefresh" min="10" max="900" message="Give value between 10 and 900 seconds">
</figure>
Is it achievable by JQuery or at least pure HTML?
You are so close. The problem is in your line autoRefresh.find('input,select,radio').val()= null ;. This is not valid jQuery. Instead, try replacing that line with this:
autoRefresh.find('input,select,radio').val('');
This clears the input value. Full working code:
$("input[name=live_stream]").on('click', function() {
var autoRefresh = $('#autorefresh');
if ($(this).val() == "No") {
autoRefresh.show();
autoRefresh.find('input,select,radio').prop('disabled', false);
} else {
autoRefresh.find('input,select,radio').prop('disabled', true);
autoRefresh.find('input,select,radio').val('');
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<figure class="question">
<label>
<div class="order">5</div>
<p class="question_title">Live stream<span class="asterisk">*</span></p>
</label>
<br>
<input name="live_stream" class="radiobtn" type="radio" value="Yes">Yes
<input name="live_stream" class="radiobtn" type="radio" value="No">No
<br>
</figure>
<figure class="question" id="autorefresh">
<label>
<div class="order">6</div>
<p class="question_title">Autorefresh frequency (in seconds)<span class="asterisk">*</span></p>
</label>
<input type="number" id="autoref" name="autorefresh" min="10" max="900" message="Give value between 10 and 900 seconds">
</figure>
I have two checkbox and under this when tick one of two checkbox I have two another option checkbox and another tick there will be text. The text will be value from the checkbox, how can I get the value from this checkbox? But the text I want to put are different sentence for each option checkbox selected not the word from checkbox. Did anyone know?
And my another problem is the checkbox not working if I do not tick the first checkbox.
<script type="text/javascript">
function ShowHideDiv(Cat) {
var dvCat = document.getElementById("bigCat");
bigCat.style.display = Cat.checked ? "block" : "none";
}
</script>
<label for="Cat">
<input type="checkbox" id="Cat" onclick="ShowHideDiv(this)" /> Cat
</label>
<script type="text/javascript">
function ShowHideDiv2(Rabbit) {
var bigRabbit = document.getElementById("bigRabbit");
bigRabbit.style.display = Rabbit.checked ? "block" : "none";
}
</script>
<label for="Rabbit">
<input type="checkbox" id="Rabbit" onclick="ShowHideDiv2(this)" /> Rabbit
</label>
<div id="bigCat" style="display: none">
<label>
<input type="checkbox" id="bigSubCat" /> British shorthair
</label>
<label>
<input type="checkbox" id="bigSubCat" /> Exotic Shorthair
</label>
<div id="bigRabbit" style="display: none">
<label>
<input type="checkbox" id="bigSubRabbit" /> White Rabbit
</label>
<label>
<input type="checkbox" id="bigSubRabbit" /> Black Rabbit
</label>
Add value attribute to the checkbox. and set value to the checkbox.Or you can take the text from the label
<script type="text/javascript">
function ShowHideDiv(Cat) {
var dvCat = document.getElementById("bigCat");
bigCat.style.display = Cat.checked ? "block" : "none";
console.log(Cat.value)
console.log("Text Inside LABEL:" + Cat.parentNode.textContent )
}
</script>
<label for="Cat">
<input type="checkbox" id="Cat" onclick="ShowHideDiv(this)" value="cat"/> Cat
</label>
<script type="text/javascript">
function ShowHideDiv2(Rabbit) {
var bigRabbit = document.getElementById("bigRabbit");
bigRabbit.style.display = Rabbit.checked ? "block" : "none";
console.log(Rabbit.value)
console.log("Text Inside LABEL:" + Rabbit.parentNode.textContent )
}
</script>
<label for="Rabbit">
<input type="checkbox" id="Rabbit" onclick="ShowHideDiv2(this)" value="Rabbit"/> Rabbit
</label>
<div id="bigCat" style="display: none">
<label>
<input type="checkbox" id="bigSubCat" /> British shorthair
</label>
<label>
<input type="checkbox" id="bigSubCat" /> Exotic Shorthair
</label>
</div>
<div id="bigRabbit" style="display: none">
<label>
<input type="checkbox" id="bigSubRabbit" /> White Rabbit
</label>
<label>
<input type="checkbox" id="bigSubRabbit" /> Black Rabbit
</label>
I think you can follow this simple logic:
https://jsfiddle.net/pablodarde/6p7zkfwf/
HTML
<div class="container">
<div class="row">
<input type="checkbox" value="This is option one!" id="opt1"><label for="opt1">Option One</label>
<p></p>
</div>
<div class="row">
<input type="checkbox" value="This is option two!" id="opt2"><label for="opt2">Option Two</label>
<p></p>
</div>
</div>
JavaScript
const checkboxes = document.querySelectorAll('.container input[type=checkbox]');
for (let i = 0, l = checkboxes.length; i < l; i++) {
checkboxes[i].addEventListener('click', (e) => {
if(checkboxes[i].parentNode.querySelector('p').innerHTML == "") {
checkboxes[i].parentNode.querySelector('p').innerHTML = checkboxes[i].value;
} else {
checkboxes[i].parentNode.querySelector('p').innerHTML = "";
}
});
}
CSS
.row {
padding-bottom: 10px;
border-bottom: 1px solid black;
}
These are my Radio Buttons:
<input type="radio" name="qualitative" value="qualitative1" ng-model="showDiv">Show
<input type="radio" name="qualitative" value="qualitative2" ng-model="hideDiv">Hide
This is the div to be hide and show:
<div id="mark_me_visible">
<h1>I am Visible</h1>
</div>
Please help me with this
Thanks in advance.
<input type="radio" name="qualitative" value="show" ng-model="showDiv">Show
<input type="radio" name="qualitative" value="hide" ng-model="showDiv">Hide
<div id="mark_me_visible" ng-show="showDiv === 'show'">
<h1>I am Visible</h1>
</div>
and in controller:
$scope.showDiv = true;
<input type="radio" name="qualitative" value="qualitative1" ng-model="showDiv" ng-click="show()">Show
<input type="radio" name="qualitative" value="qualitative2" ng-model="hideDiv" ng-click="hide()">Hide
In the controller
$scope.show = function(){
$scope.show_div = true
}
$scope.hide = function(){
$scope.show_div = false
}
In the div
<div id="mark_me_visible" ng-if=!show_div>
<h1>I am Visible</h1>
</div>
Try this. Since we are using angularjs you have to define module and controller as I have done below.
var app = angular.module('IApp', []);
app.controller('formCtrl', function($scope) {
});
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<div ng-app="IApp" ng-controller="formCtrl">
<input type="radio" name="qualitative" ng-init="showDiv=true" checked="true" ng-click="showDiv = !showDiv">Show
<input type="radio" name="qualitative" ng-init="showDiv=true" ng-click="showDiv = !showDiv">Hide
<div id="Div1" ng-model="showDiv" ng-init="showDiv=true" ng-show="showDiv">
<h1>I am Visible {{showDiv}}</h1>
</div>
</div>
for this jsfiddle - http://jsfiddle.net/Ja3Fx/1/ $('div').trigger('create') doesn't
work as required.
it does style radio button but it isn't quite right ... any ideas ?
<div id="testPage" data-role="page">
<div data-role="content">
<fieldset data-role="controlgroup">
<legend>Radio buttons, vertical controlgroup:</legend>
<input type="radio" name="radio-choice-1" id="radio-choice-1" value="choice-1" checked="checked">
<label for="radio-choice-1">Cat</label>
<input type="radio" name="radio-choice-1" id="radio-choice-2" value="choice-2">
<label for="radio-choice-2">Dog</label>
<input type="radio" name="radio-choice-1" id="radio-choice-3" value="choice-3">
<label for="radio-choice-3">Hamster</label>
<input type="radio" name="radio-choice-1" id="radio-choice-4" value="choice-4">
<label for="radio-choice-4">Lizard</label>
</fieldset>
<a id="add-animal" href="#" data-role="button" data-position-to="window">Add Animal</a>
</div>
</div>
$('#add-animal').click(function() {
var fldset = $('fieldset');
$(fldset).append($('<input type="radio" name="radio-choice-1" id="radio-choice-5" value="choice-5"><label for="radio-choice-5">Bat</label>'));
$("input").checkboxradio();
$('div ').trigger('create ');
});
I am using the following code to make a panel. You can try similar code.
$(document).on('pagecreate', '[data-role="page"]', function(){
$('<div>').attr({'id':'mypanel','data-role':'panel'}).appendTo($(this));
$(document).on('click', '#open-panel', function(){
$.mobile.activePage.find('#mypanel').panel("open");
});
});
dear i have 2 text and 2 checkbox:
<input type="checkbox" id="class1">
<input type="checkbox" id="class2">
<div id="hidden">
<input type="text" id="valclass1">
<input type="text" id="valclass2">
</div>
i want if i checked at "class1" then click show button "valclass1" can show. but if not checked, it hidden.how do i do that?
I would do it like this:
<script type="javascript/text" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="javascript/text">
$(function()
{
$('#ShowButton').click(function()
{
if($('#class1').is(':checked')) $('#valclass1').show();
if($('#class2').is(':checked')) $('#valclass2').show();
});
});
</script>
<input type="checkbox" id="class1">
<input type="checkbox" id="class2">
<input type="button" id="ShowButton" />
<input type="text" id="valclass1" style="display: none">
<input type="text" id="valclass2" style="display: none">
It would work, but probably doesn't answer your question. You need to learn more about JavaScript to be able to roll your own solution.
Check out W3C's JavaScript tutorial